p_mobj.c 252 KB
Newer Older
Alam Ed Arias's avatar
Alam Ed Arias committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 1998-2000 by DooM Legacy Team.
// Copyright (C) 1999-2014 by Sonic Team Junior.
//
// 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_mobj.c
/// \brief Moving object handling. Spawn functions

#include "doomdef.h"
#include "g_game.h"
#include "g_input.h"
#include "st_stuff.h"
#include "hu_stuff.h"
#include "p_local.h"
#include "p_setup.h"
#include "r_main.h"
#include "r_things.h"
#include "r_sky.h"
#include "r_splats.h"
#include "s_sound.h"
#include "z_zone.h"
#include "m_random.h"
#include "m_cheat.h"
#include "m_misc.h"
#include "info.h"
#include "i_video.h"
#include "lua_hook.h"
#include "b_bot.h"

// protos.
static CV_PossibleValue_t viewheight_cons_t[] = {{16, "MIN"}, {56, "MAX"}, {0, NULL}};
consvar_t cv_viewheight = {"viewheight", VIEWHEIGHTS, 0, viewheight_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL};
consvar_t cv_splats = {"splats", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL};

actioncache_t actioncachehead;

static mobj_t *overlaycap = NULL;

void P_InitCachedActions(void)
{
	actioncachehead.prev = actioncachehead.next = &actioncachehead;
}

void P_RunCachedActions(void)
{
	actioncache_t *ac;
	actioncache_t *next;

	for (ac = actioncachehead.next; ac != &actioncachehead; ac = next)
	{
		var1 = states[ac->statenum].var1;
		var2 = states[ac->statenum].var2;
#ifdef HAVE_BLUA
		astate = &states[ac->statenum];
#endif
Alam Ed Arias's avatar
Alam Ed Arias committed
61
		if (ac->mobj && !P_MobjWasRemoved(ac->mobj)) // just in case...
Alam Ed Arias's avatar
Alam Ed Arias committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
			states[ac->statenum].action.acp1(ac->mobj);
		next = ac->next;
		Z_Free(ac);
	}
}

void P_AddCachedAction(mobj_t *mobj, INT32 statenum)
{
	actioncache_t *newaction = Z_Calloc(sizeof(actioncache_t), PU_LEVEL, NULL);
	newaction->mobj = mobj;
	newaction->statenum = statenum;
	actioncachehead.prev->next = newaction;
	newaction->next = &actioncachehead;
	newaction->prev = actioncachehead.prev;
	actioncachehead.prev = newaction;
}

//
// P_CycleMobjState
//
static void P_CycleMobjState(mobj_t *mobj)
{
	// cycle through states,
	// calling action functions at transitions
	if (mobj->tics != -1)
	{
		mobj->tics--;

		// you can cycle through multiple states in a tic
		if (!mobj->tics && mobj->state)
			if (!P_SetMobjState(mobj, mobj->state->nextstate))
				return; // freed itself
	}
}

//
// P_CycleMobjState for players.
//
static void P_CyclePlayerMobjState(mobj_t *mobj)
{
	// cycle through states,
	// calling action functions at transitions
	if (mobj->tics != -1)
	{
		mobj->tics--;

		// you can cycle through multiple states in a tic
		if (!mobj->tics && mobj->state)
			if (!P_SetPlayerMobjState(mobj, mobj->state->nextstate))
				return; // freed itself
	}
}

//
// P_SetPlayerMobjState
// Returns true if the mobj is still present.
//
// Separate from P_SetMobjState because of the pw_flashing check and Super states
//
boolean P_SetPlayerMobjState(mobj_t *mobj, statenum_t state)
{
	state_t *st;
	player_t *player = mobj->player;

	// remember states seen, to detect cycles:
	static statenum_t seenstate_tab[NUMSTATES]; // fast transition table
	statenum_t *seenstate = seenstate_tab; // pointer to table
	static INT32 recursion; // detects recursion
	statenum_t i; // initial state
	statenum_t tempstate[NUMSTATES]; // for use with recursion

#ifdef PARANOIA
	if (player == NULL)
		I_Error("P_SetPlayerMobjState used for non-player mobj. Use P_SetMobjState instead!\n(Mobj type: %d, State: %d)", mobj->type, state);
#endif

	// Catch state changes for Super Sonic
	if (player->powers[pw_super] && (player->charflags & SF_SUPERANIMS))
	{
		switch (state)
		{
		case S_PLAY_STND:
		case S_PLAY_TAP1:
		case S_PLAY_TAP2:
		case S_PLAY_GASP:
			P_SetPlayerMobjState(mobj, S_PLAY_SUPERSTAND);
			return true;
		case S_PLAY_FALL1:
		case S_PLAY_SPRING:
		case S_PLAY_RUN1:
		case S_PLAY_RUN2:
		case S_PLAY_RUN3:
		case S_PLAY_RUN4:
			P_SetPlayerMobjState(mobj, S_PLAY_SUPERWALK1);
			return true;
		case S_PLAY_FALL2:
		case S_PLAY_RUN5:
		case S_PLAY_RUN6:
		case S_PLAY_RUN7:
		case S_PLAY_RUN8:
			P_SetPlayerMobjState(mobj, S_PLAY_SUPERWALK2);
			return true;
		case S_PLAY_SPD1:
		case S_PLAY_SPD2:
			P_SetPlayerMobjState(mobj, S_PLAY_SUPERFLY1);
			return true;
		case S_PLAY_SPD3:
		case S_PLAY_SPD4:
			P_SetPlayerMobjState(mobj, S_PLAY_SUPERFLY2);
			return true;
		case S_PLAY_TEETER1:
		case S_PLAY_TEETER2:
			P_SetPlayerMobjState(mobj, S_PLAY_SUPERTEETER);
			return true;
		case S_PLAY_ATK1:
		case S_PLAY_ATK2:
		case S_PLAY_ATK3:
		case S_PLAY_ATK4:
			if (!(player->charflags & SF_SUPERSPIN))
				return true;
			break;
		default:
			break;
		}
	}
	// You were in pain state after taking a hit, and you're moving out of pain state now?
	else if (mobj->state == &states[mobj->info->painstate] && player->powers[pw_flashing] == flashingtics && state != mobj->info->painstate)
	{
		// Start flashing, since you've landed.
		player->powers[pw_flashing] = flashingtics-1;
		P_DoPityCheck(player);
	}

	// Set animation state
	// The pflags version of this was just as convoluted.
	if ((state >= S_PLAY_STND && state <= S_PLAY_TAP2) || (state >= S_PLAY_TEETER1 && state <= S_PLAY_TEETER2) || state == S_PLAY_CARRY
	|| state == S_PLAY_SUPERSTAND || state == S_PLAY_SUPERTEETER)
		player->panim = PA_IDLE;
	else if ((state >= S_PLAY_RUN1 && state <= S_PLAY_RUN8)
	|| (state >= S_PLAY_SUPERWALK1 && state <= S_PLAY_SUPERWALK2))
		player->panim = PA_WALK;
	else if ((state >= S_PLAY_SPD1 && state <= S_PLAY_SPD4)
	|| (state >= S_PLAY_SUPERFLY1 && state <= S_PLAY_SUPERFLY2))
		player->panim = PA_RUN;
	else if (state >= S_PLAY_ATK1 && state <= S_PLAY_ATK4)
		player->panim = PA_ROLL;
	else if (state >= S_PLAY_FALL1 && state <= S_PLAY_FALL2)
		player->panim = PA_FALL;
	else if (state >= S_PLAY_ABL1 && state <= S_PLAY_ABL2)
		player->panim = PA_ABILITY;
	else
		player->panim = PA_ETC;

	if (recursion++) // if recursion detected,
		memset(seenstate = tempstate, 0, sizeof tempstate); // clear state table

	i = state;

	do
	{
		if (state == S_NULL)
		{ // Bad SOC!
			CONS_Alert(CONS_ERROR, "Cannot remove player mobj by setting its state to S_NULL.\n");
			//P_RemoveMobj(mobj);
			return false;
		}

		st = &states[state];
		mobj->state = st;
		mobj->tics = st->tics;

		// Adjust the player's animation speed to match their velocity.
		if (!(disableSpeedAdjust || player->charflags & SF_NOSPEEDADJUST))
		{
			fixed_t speed = FixedDiv(player->speed, mobj->scale);
			if (player->panim == PA_ROLL)
			{
				if (speed > 16<<FRACBITS)
					mobj->tics = 1;
				else
					mobj->tics = 2;
			}
			else if (player->panim == PA_FALL)
			{
				speed = FixedDiv(abs(mobj->momz), mobj->scale);
				if (speed < 10<<FRACBITS)
					mobj->tics = 4;
				else if (speed < 20<<FRACBITS)
					mobj->tics = 3;
				else if (speed < 30<<FRACBITS)
					mobj->tics = 2;
				else
					mobj->tics = 1;
			}
			else if (P_IsObjectOnGround(mobj) || player->powers[pw_super]) // Only if on the ground or superflying.
			{
				if (player->panim == PA_WALK)
				{
					if (speed > 12<<FRACBITS)
						mobj->tics = 2;
					else if (speed > 6<<FRACBITS)
						mobj->tics = 3;
					else
						mobj->tics = 4;
				}
				else if (player->panim == PA_RUN)
				{
					if (speed > 52<<FRACBITS)
						mobj->tics = 1;
					else
						mobj->tics = 2;
				}
			}
		}

		mobj->sprite = st->sprite;
		mobj->frame = st->frame;

		// Modified handling.
		// Call action functions when the state is set

		if (st->action.acp1)
		{
			var1 = st->var1;
			var2 = st->var2;
#ifdef HAVE_BLUA
			astate = st;
#endif
			st->action.acp1(mobj);
Alam Ed Arias's avatar
Alam Ed Arias committed
291 292 293 294 295

			// woah. a player was removed by an action.
			// this sounds like a VERY BAD THING, but there's nothing we can do now...
			if (P_MobjWasRemoved(mobj))
				return false;
Alam Ed Arias's avatar
Alam Ed Arias committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
		}

		seenstate[state] = 1 + st->nextstate;

		state = st->nextstate;
	} while (!mobj->tics && !seenstate[state]);

	if (!mobj->tics)
		CONS_Alert(CONS_WARNING, M_GetText("State cycle detected, exiting.\n"));

	if (!--recursion)
		for (;(state = seenstate[i]) > S_NULL; i = state - 1)
			seenstate[i] = S_NULL; // erase memory of states

	return true;
}


boolean P_SetMobjState(mobj_t *mobj, statenum_t state)
{
	state_t *st;

	// remember states seen, to detect cycles:
	static statenum_t seenstate_tab[NUMSTATES]; // fast transition table
	statenum_t *seenstate = seenstate_tab; // pointer to table
	static INT32 recursion; // detects recursion
	statenum_t i = state; // initial state
	statenum_t tempstate[NUMSTATES]; // for use with recursion

#ifdef PARANOIA
	if (mobj->player != NULL)
		I_Error("P_SetMobjState used for player mobj. Use P_SetPlayerMobjState instead!\n(State called: %d)", state);
#endif

	if (recursion++) // if recursion detected,
		memset(seenstate = tempstate, 0, sizeof tempstate); // clear state table

	do
	{
		if (state == S_NULL)
		{
			P_RemoveMobj(mobj);
			return false;
		}

		st = &states[state];
		mobj->state = st;
		mobj->tics = st->tics;
		mobj->sprite = st->sprite;
		mobj->frame = st->frame;

		// Modified handling.
		// Call action functions when the state is set

		if (st->action.acp1)
		{
			var1 = st->var1;
			var2 = st->var2;
#ifdef HAVE_BLUA
			astate = st;
#endif
			st->action.acp1(mobj);
			if (P_MobjWasRemoved(mobj))
				return false;
		}

		seenstate[state] = 1 + st->nextstate;

		state = st->nextstate;
	} while (!mobj->tics && !seenstate[state]);

	if (!mobj->tics)
		CONS_Alert(CONS_WARNING, M_GetText("State cycle detected, exiting.\n"));

	if (!--recursion)
		for (;(state = seenstate[i]) > S_NULL; i = state - 1)
			seenstate[i] = S_NULL; // erase memory of states

	return true;
}

//----------------------------------------------------------------------------
//
// FUNC P_SetMobjStateNF
//
// Same as P_SetMobjState, but does not call the state function.
//
//----------------------------------------------------------------------------

boolean P_SetMobjStateNF(mobj_t *mobj, statenum_t state)
{
	state_t *st;

	if (state == S_NULL)
	{ // Remove mobj
		P_RemoveMobj(mobj);
		return false;
	}
	st = &states[state];
	mobj->state = st;
	mobj->tics = st->tics;
	mobj->sprite = st->sprite;
	mobj->frame = st->frame;
	return true;
}

static boolean P_SetPrecipMobjState(precipmobj_t *mobj, statenum_t state)
{
	state_t *st;

	if (state == S_NULL)
	{ // Remove mobj
		P_RemovePrecipMobj(mobj);
		return false;
	}
	st = &states[state];
	mobj->state = st;
	mobj->tics = st->tics;
	mobj->sprite = st->sprite;
	mobj->frame = st->frame;
	return true;
}

//
// P_MobjFlip
//
// Special utility to return +1 or -1 depending on mobj's gravity
//
SINT8 P_MobjFlip(mobj_t *mobj)
{
	if (mobj && mobj->eflags & MFE_VERTICALFLIP)
		return -1;
	return 1;
}

//
// P_WeaponOrPanel
//
// Returns true if weapon ring/panel; otherwise returns false
//
boolean P_WeaponOrPanel(mobjtype_t type)
{
	if (type == MT_BOUNCERING
	|| type == MT_AUTOMATICRING
	|| type == MT_INFINITYRING
	|| type == MT_RAILRING
	|| type == MT_EXPLOSIONRING
	|| type == MT_SCATTERRING
	|| type == MT_GRENADERING
	|| type == MT_BOUNCEPICKUP
	|| type == MT_RAILPICKUP
	|| type == MT_AUTOPICKUP
	|| type == MT_EXPLODEPICKUP
	|| type == MT_SCATTERPICKUP
	|| type == MT_GRENADEPICKUP)
		return true;

	return false;
}

//
// P_EmeraldManager
//
// Power Stone emerald management
//
void P_EmeraldManager(void)
{
	thinker_t *think;
	mobj_t *mo;
	INT32 i,j;
	INT32 numtospawn;
	INT32 emeraldsspawned = 0;

	boolean hasemerald[MAXHUNTEMERALDS];
	INT32 numwithemerald = 0;

	// record empty spawn points
	mobj_t *spawnpoints[MAXHUNTEMERALDS];
	INT32 numspawnpoints = 0;

	for (i = 0; i < MAXHUNTEMERALDS; i++)
	{
		hasemerald[i] = false;
		spawnpoints[i] = NULL;
	}

	for (think = thinkercap.next; think != &thinkercap; think = think->next)
	{
		if (think->function.acp1 != (actionf_p1)P_MobjThinker)
			continue; // not a mobj thinker

		mo = (mobj_t *)think;

		if (mo->type == MT_EMERALDSPAWN)
		{
			if (mo->threshold || mo->target) // Either has the emerald spawned or is spawning
			{
				numwithemerald++;
				emeraldsspawned |= mobjinfo[mo->reactiontime].speed;
			}
			else if (numspawnpoints < MAXHUNTEMERALDS)
				spawnpoints[numspawnpoints++] = mo; // empty spawn points
		}
		else if (mo->type == MT_FLINGEMERALD)
		{
			numwithemerald++;
			emeraldsspawned |= mo->threshold;
		}
	}

	if (numspawnpoints == 0)
		return;

	// But wait! We need to check all the players too, to see if anyone has some of the emeralds.
	for (i = 0; i < MAXPLAYERS; i++)
	{
		if (!playeringame[i] || players[i].spectator)
			continue;

		if (!players[i].mo)
			continue;

		if (players[i].powers[pw_emeralds] & EMERALD1)
		{
			numwithemerald++;
			emeraldsspawned |= EMERALD1;
		}
		if (players[i].powers[pw_emeralds] & EMERALD2)
		{
			numwithemerald++;
			emeraldsspawned |= EMERALD2;
		}
		if (players[i].powers[pw_emeralds] & EMERALD3)
		{
			numwithemerald++;
			emeraldsspawned |= EMERALD3;
		}
		if (players[i].powers[pw_emeralds] & EMERALD4)
		{
			numwithemerald++;
			emeraldsspawned |= EMERALD4;
		}
		if (players[i].powers[pw_emeralds] & EMERALD5)
		{
			numwithemerald++;
			emeraldsspawned |= EMERALD5;
		}
		if (players[i].powers[pw_emeralds] & EMERALD6)
		{
			numwithemerald++;
			emeraldsspawned |= EMERALD6;
		}
		if (players[i].powers[pw_emeralds] & EMERALD7)
		{
			numwithemerald++;
			emeraldsspawned |= EMERALD7;
		}
	}

	// All emeralds spawned, no worries
	if (numwithemerald >= 7)
		return;

	// Set up spawning for the emeralds
	numtospawn = 7 - numwithemerald;

#ifdef PARANOIA
	if (numtospawn <= 0) // ???
		I_Error("P_EmeraldManager: numtospawn is %d!\n", numtospawn);
#endif

	for (i = 0, j = 0; i < numtospawn; i++)
	{
		INT32 tries = 0;
		while (true)
		{
			tries++;

			if (tries > 50)
				break;

			j = P_RandomKey(numspawnpoints);

			if (hasemerald[j])
				continue;

			hasemerald[j] = true;

			if (!(emeraldsspawned & EMERALD1))
			{
				spawnpoints[j]->reactiontime = MT_EMERALD1;
				emeraldsspawned |= EMERALD1;
			}
			else if (!(emeraldsspawned & EMERALD2))
			{
				spawnpoints[j]->reactiontime = MT_EMERALD2;
				emeraldsspawned |= EMERALD2;
			}
			else if (!(emeraldsspawned & EMERALD3))
			{
				spawnpoints[j]->reactiontime = MT_EMERALD3;
				emeraldsspawned |= EMERALD3;
			}
			else if (!(emeraldsspawned & EMERALD4))
			{
				spawnpoints[j]->reactiontime = MT_EMERALD4;
				emeraldsspawned |= EMERALD4;
			}
			else if (!(emeraldsspawned & EMERALD5))
			{
				spawnpoints[j]->reactiontime = MT_EMERALD5;
				emeraldsspawned |= EMERALD5;
			}
			else if (!(emeraldsspawned & EMERALD6))
			{
				spawnpoints[j]->reactiontime = MT_EMERALD6;
				emeraldsspawned |= EMERALD6;
			}
			else if (!(emeraldsspawned & EMERALD7))
			{
				spawnpoints[j]->reactiontime = MT_EMERALD7;
				emeraldsspawned |= EMERALD7;
			}
			else
				break;

			if (leveltime < TICRATE) // Start of map
				spawnpoints[j]->threshold = 60*TICRATE + P_Random() * (TICRATE/5);
			else
				spawnpoints[j]->threshold = P_Random() * (TICRATE/5);

			break;
		}
	}
}

//
// P_ExplodeMissile
//
void P_ExplodeMissile(mobj_t *mo)
{
	mobj_t *explodemo;

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	mo->momx = mo->momy = mo->momz = 0;

	if (mo->flags & MF_NOCLIPTHING)
		return;

	if (mo->type == MT_DETON)
	{
		P_RadiusAttack(mo, mo, 96*FRACUNIT);

		explodemo = P_SpawnMobj(mo->x, mo->y, mo->z, MT_EXPLODE);
		P_SetScale(explodemo, mo->scale);
		explodemo->destscale = mo->destscale;
		explodemo->momx += (P_Random() % 32) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy += (P_Random() % 32) * FixedMul(FRACUNIT/8, explodemo->scale);
		S_StartSound(explodemo, sfx_pop);
		explodemo = P_SpawnMobj(mo->x, mo->y, mo->z, MT_EXPLODE);
		P_SetScale(explodemo, mo->scale);
		explodemo->destscale = mo->destscale;
		explodemo->momx += (P_Random() % 64) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy -= (P_Random() % 64) * FixedMul(FRACUNIT/8, explodemo->scale);
		S_StartSound(explodemo, sfx_dmpain);
		explodemo = P_SpawnMobj(mo->x, mo->y, mo->z, MT_EXPLODE);
		P_SetScale(explodemo, mo->scale);
		explodemo->destscale = mo->destscale;
		explodemo->momx -= (P_Random() % 128) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy += (P_Random() % 128) * FixedMul(FRACUNIT/8, explodemo->scale);
		S_StartSound(explodemo, sfx_pop);
		explodemo = P_SpawnMobj(mo->x, mo->y, mo->z, MT_EXPLODE);
		P_SetScale(explodemo, mo->scale);
		explodemo->destscale = mo->destscale;
		explodemo->momx -= (P_Random() % 96) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy -= (P_Random() % 96) * FixedMul(FRACUNIT/8, explodemo->scale);
		S_StartSound(explodemo, sfx_cybdth);
Alam Ed Arias's avatar
Alam Ed Arias committed
675 676 677

		// Hack: Release an animal.
		P_DamageMobj(mo, NULL, NULL, 10000);
Alam Ed Arias's avatar
Alam Ed Arias committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	}

	mo->flags &= ~MF_MISSILE;

	mo->flags |= MF_NOGRAVITY; // Dead missiles don't need to sink anymore.
	mo->flags |= MF_NOCLIPTHING; // Dummy flag to indicate that this was already called.

	if (mo->info->deathsound && !(mo->flags2 & MF2_DEBRIS))
		S_StartSound(mo, mo->info->deathsound);

	P_SetMobjState(mo, mo->info->deathstate);
}

// P_InsideANonSolidFFloor
//
// Returns TRUE if mobj is inside a non-solid 3d floor.
boolean P_InsideANonSolidFFloor(mobj_t *mobj, ffloor_t *rover)
{
	if (!(rover->flags & FF_EXISTS))
		return false;

	if ((((rover->flags & FF_BLOCKPLAYER) && mobj->player)
		|| ((rover->flags & FF_BLOCKOTHERS) && !mobj->player)))
		return false;

	if (mobj->z > *rover->topheight)
		return false;

	if (mobj->z + mobj->height < *rover->bottomheight)
		return false;

	return true;
}

Alam Ed Arias's avatar
Alam Ed Arias committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
static void P_PlayerFlip(mobj_t *mo)
{
	if (!mo->player)
		return;

	G_GhostAddFlip();
	// Flip aiming to match!

	if (mo->player->pflags & PF_NIGHTSMODE) // NiGHTS doesn't use flipcam
	{
		if (mo->tracer)
			mo->tracer->eflags ^= MFE_VERTICALFLIP;
	}
	else if (mo->player->pflags & PF_FLIPCAM)
	{
		mo->player->aiming = InvAngle(mo->player->aiming);
		if (mo->player-players == displayplayer)
		{
			localaiming = mo->player->aiming;
			if (camera.chase) {
				camera.aiming = InvAngle(camera.aiming);
				camera.z = mo->z - camera.z + mo->z;
				if (mo->eflags & MFE_VERTICALFLIP)
					camera.z += FixedMul(20*FRACUNIT, mo->scale);
			}
		}
		else if (mo->player-players == secondarydisplayplayer)
		{
			localaiming2 = mo->player->aiming;
			if (camera2.chase) {
				camera2.aiming = InvAngle(camera2.aiming);
				camera2.z = mo->z - camera2.z + mo->z;
				if (mo->eflags & MFE_VERTICALFLIP)
					camera2.z += FixedMul(20*FRACUNIT, mo->scale);
			}
		}
	}
}

Alam Ed Arias's avatar
Alam Ed Arias committed
751 752 753 754 755 756 757 758 759 760 761 762
//
// P_CheckGravity
//
// Checks the current gravity state
// of the object. If affect is true,
// a gravity force will be applied.
//
void P_CheckGravity(mobj_t *mo, boolean affect)
{
	fixed_t gravityadd = 0;
	boolean no3dfloorgrav = true; // Custom gravity
	boolean goopgravity = false;
Alam Ed Arias's avatar
Alam Ed Arias committed
763
	boolean wasflip;
Alam Ed Arias's avatar
Alam Ed Arias committed
764 765 766 767

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

Alam Ed Arias's avatar
Alam Ed Arias committed
768 769
	wasflip = (mo->eflags & MFE_VERTICALFLIP) != 0;

Alam Ed Arias's avatar
Alam Ed Arias committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
	if (mo->type != MT_SPINFIRE)
		mo->eflags &= ~MFE_VERTICALFLIP;

	if (mo->subsector->sector->ffloors) // Check for 3D floor gravity too.
	{
		ffloor_t *rover;

		for (rover = mo->subsector->sector->ffloors; rover; rover = rover->next)
		{
			if (!(rover->flags & FF_EXISTS))
				continue;

			if (P_InsideANonSolidFFloor(mo, rover))
			{
				if ((rover->flags & (FF_SWIMMABLE|FF_GOOWATER)) == (FF_SWIMMABLE|FF_GOOWATER))
					goopgravity = true;
				if (rover->master->frontsector->gravity)
				{
					gravityadd = -FixedMul(gravity,
						(FixedDiv(*rover->master->frontsector->gravity>>FRACBITS, 1000)));

					if (rover->master->frontsector->verticalflip && gravityadd > 0)
						mo->eflags |= MFE_VERTICALFLIP;

					no3dfloorgrav = false;
					break;
				}
			}
		}
	}

	if (no3dfloorgrav)
	{
		if (mo->subsector->sector->gravity)
			gravityadd = -FixedMul(gravity,
				(FixedDiv(*mo->subsector->sector->gravity>>FRACBITS, 1000)));
		else
			gravityadd = -gravity;

		if (mo->subsector->sector->verticalflip && gravityadd > 0)
			mo->eflags |= MFE_VERTICALFLIP;
	}

	// Less gravity underwater.
	if (mo->eflags & MFE_UNDERWATER && !goopgravity)
		gravityadd = gravityadd/3;

	if (!mo->momz) // mobj at stop, no floor, so feel the push of gravity!
		gravityadd <<= 1;

	if (mo->player)
	{
		if (mo->player->charability == CA_FLY && (mo->player->powers[pw_tailsfly]
		|| (mo->state >= &states[S_PLAY_SPC1] && mo->state <= &states[S_PLAY_SPC4])))
			gravityadd = gravityadd/3; // less gravity while flying
		if (mo->player->pflags & PF_GLIDING)
			gravityadd = gravityadd/3; // less gravity while gliding
		if (mo->player->climbing)
			gravityadd = 0;
		if (mo->player->pflags & PF_NIGHTSMODE)
			gravityadd = 0;

		{
			UINT8 bits = 0;
			if (mo->flags2 & MF2_OBJECTFLIP)
				bits ^= 1;
			if (mo->player->powers[pw_gravityboots])
				bits ^= 1;
			if (bits & 1)
			{
				gravityadd = -gravityadd;
Alam Ed Arias's avatar
Alam Ed Arias committed
841
				mo->eflags ^= MFE_VERTICALFLIP;
Alam Ed Arias's avatar
Alam Ed Arias committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
			}
		}
	}
	else
	{
		// Objects with permanent reverse gravity (MF2_OBJECTFLIP)
		if (mo->flags2 & MF2_OBJECTFLIP)
		{
			mo->eflags |= MFE_VERTICALFLIP;
			if (gravityadd < 0) // Don't sink, only rise up
				gravityadd *= -1;
			if (mo->z + mo->height >= mo->ceilingz)
				gravityadd = 0;
		}
		else //Otherwise, sort through the other exceptions.
		{
			switch (mo->type)
			{
				case MT_FLINGRING:
				case MT_FLINGCOIN:
				case MT_FLINGEMERALD:
				case MT_BOUNCERING:
				case MT_RAILRING:
				case MT_INFINITYRING:
				case MT_AUTOMATICRING:
				case MT_EXPLOSIONRING:
				case MT_SCATTERRING:
				case MT_GRENADERING:
				case MT_BOUNCEPICKUP:
				case MT_RAILPICKUP:
				case MT_AUTOPICKUP:
				case MT_EXPLODEPICKUP:
				case MT_SCATTERPICKUP:
				case MT_GRENADEPICKUP:
				case MT_REDFLAG:
				case MT_BLUEFLAG:
					if (mo->target)
					{
						// Flung items copy the gravity of their tosser.
						if ((mo->target->eflags & MFE_VERTICALFLIP) && !(mo->eflags & MFE_VERTICALFLIP))
						{
							gravityadd = -gravityadd;
							mo->eflags |= MFE_VERTICALFLIP;
						}
					}
					break;
				case MT_WATERDROP:
					gravityadd >>= 1;
				default:
					break;
			}
		}
	}

	// Goop has slower, reversed gravity
	if (goopgravity)
		gravityadd = -gravityadd/5;

	if (affect)
		mo->momz += FixedMul(gravityadd, mo->scale);

Alam Ed Arias's avatar
Alam Ed Arias committed
903 904 905
	if (mo->player && !!(mo->eflags & MFE_VERTICALFLIP) != wasflip)
		P_PlayerFlip(mo);

Alam Ed Arias's avatar
Alam Ed Arias committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
	if (mo->type == MT_SKIM && mo->z + mo->momz <= mo->watertop && mo->z >= mo->watertop)
	{
		mo->momz = 0;
		mo->flags |= MF_NOGRAVITY;
	}
}

#define STOPSPEED (FRACUNIT)
#define FRICTION (ORIG_FRICTION) // 0.90625

//
// P_SceneryXYFriction
//
static void P_SceneryXYFriction(mobj_t *mo, fixed_t oldx, fixed_t oldy)
{
	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	if (abs(mo->momx) < FixedMul(STOPSPEED/32, mo->scale)
		&& abs(mo->momy) < FixedMul(STOPSPEED/32, mo->scale))
	{
		mo->momx = 0;
		mo->momy = 0;
	}
	else
	{
		if ((oldx == mo->x) && (oldy == mo->y)) // didn't go anywhere
		{
			mo->momx = FixedMul(mo->momx,ORIG_FRICTION);
			mo->momy = FixedMul(mo->momy,ORIG_FRICTION);
		}
		else
		{
			mo->momx = FixedMul(mo->momx,mo->friction);
			mo->momy = FixedMul(mo->momy,mo->friction);
		}

		if (mo->type == MT_CANNONBALLDECOR)
		{
			// Stolen from P_SpawnFriction
			mo->friction = FRACUNIT - 0x100;
			mo->movefactor = ((0x10092 - mo->friction)*(0x70))/0x158;
		}
		else
			mo->friction = ORIG_FRICTION;
	}
}

//
// P_XYFriction
//
// adds friction on the xy plane
//
static void P_XYFriction(mobj_t *mo, fixed_t oldx, fixed_t oldy)
{
	player_t *player;

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	player = mo->player;
	if (player) // valid only if player avatar
	{
Alam Ed Arias's avatar
Alam Ed Arias committed
969 970 971 972 973 974 975 976 977 978
		// spinning friction
		if (player->pflags & PF_SPINNING && (player->rmomx || player->rmomy) && !(player->pflags & PF_STARTDASH))
		{
			const fixed_t ns = FixedDiv(549*FRICTION,500*FRACUNIT);
			mo->momx = FixedMul(mo->momx, ns);
			mo->momy = FixedMul(mo->momy, ns);
		}
		else if (abs(player->rmomx) < FixedMul(STOPSPEED, mo->scale)
		    && abs(player->rmomy) < FixedMul(STOPSPEED, mo->scale)
		    && (!(player->cmd.forwardmove && !(twodlevel || mo->flags2 & MF2_TWOD)) && !player->cmd.sidemove && !(player->pflags & PF_SPINNING)))
Alam Ed Arias's avatar
Alam Ed Arias committed
979 980
		{
			// if in a walking frame, stop moving
Alam Ed Arias's avatar
Alam Ed Arias committed
981 982
			if (player->panim == PA_WALK)
				P_SetPlayerMobjState(mo, S_PLAY_STND);
Alam Ed Arias's avatar
Alam Ed Arias committed
983 984 985 986 987
			mo->momx = player->cmomx;
			mo->momy = player->cmomy;
		}
		else
		{
Alam Ed Arias's avatar
Alam Ed Arias committed
988
			if (oldx == mo->x && oldy == mo->y) // didn't go anywhere
Alam Ed Arias's avatar
Alam Ed Arias committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
			{
				mo->momx = FixedMul(mo->momx, ORIG_FRICTION);
				mo->momy = FixedMul(mo->momy, ORIG_FRICTION);
			}
			else
			{
				mo->momx = FixedMul(mo->momx, mo->friction);
				mo->momy = FixedMul(mo->momy, mo->friction);
			}

			mo->friction = ORIG_FRICTION;
		}
	}
	else
		P_SceneryXYFriction(mo, oldx, oldy);
}

static void P_PushableCheckBustables(mobj_t *mo)
{
	msecnode_t *node;
	fixed_t oldx;
	fixed_t oldy;

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	if (netgame && mo->player && mo->player->spectator)
		return;

	oldx = mo->x;
	oldy = mo->y;

	P_UnsetThingPosition(mo);
	mo->x += mo->momx;
	mo->y += mo->momy;
	P_SetThingPosition(mo);

	for (node = mo->touching_sectorlist; node; node = node->m_snext)
	{
		if (!node->m_sector)
			break;

		if (node->m_sector->ffloors)
		{
			ffloor_t *rover;

			for (rover = node->m_sector->ffloors; rover; rover = rover->next)
			{
				if (!(rover->flags & FF_EXISTS)) continue;

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

				// Needs ML_EFFECT4 flag for pushables to break it
				if (!(rover->master->flags & ML_EFFECT4)) continue;

				if (!rover->master->frontsector->crumblestate)
				{
					// Height checks
					if (rover->flags & FF_SHATTERBOTTOM)
					{
						if (mo->z+mo->momz + mo->height < *rover->bottomheight)
							continue;

						if (mo->z+mo->height > *rover->bottomheight)
							continue;
					}
					else if (rover->flags & FF_SPINBUST)
					{
						if (mo->z+mo->momz > *rover->topheight)
							continue;

						if (mo->z+mo->height < *rover->bottomheight)
							continue;
					}
					else if (rover->flags & FF_SHATTER)
					{
						if (mo->z+mo->momz > *rover->topheight)
							continue;

						if (mo->z+mo->momz + mo->height < *rover->bottomheight)
							continue;
					}
					else
					{
						if (mo->z >= *rover->topheight)
							continue;

						if (mo->z+mo->height < *rover->bottomheight)
							continue;
					}

					EV_CrumbleChain(node->m_sector, rover);

					// Run a linedef executor??
					if (rover->master->flags & ML_EFFECT5)
						P_LinedefExecute((INT16)(P_AproxDistance(rover->master->dx, rover->master->dy)>>FRACBITS), mo, node->m_sector);

					goto bustupdone;
				}
			}
		}
	}
bustupdone:
	P_UnsetThingPosition(mo);
	mo->x = oldx;
	mo->y = oldy;
	P_SetThingPosition(mo);
}

//
// P_CheckSkyHit
//
static boolean P_CheckSkyHit(mobj_t *mo)
{
	if (ceilingline && ceilingline->backsector
		&& ceilingline->backsector->ceilingpic == skyflatnum
		&& ceilingline->frontsector
		&& ceilingline->frontsector->ceilingpic == skyflatnum
		&& (mo->z >= ceilingline->frontsector->ceilingheight
		|| mo->z >= ceilingline->backsector->ceilingheight))
			return true;
	return false;
}

//
// P_XYMovement
//
void P_XYMovement(mobj_t *mo)
{
	player_t *player;
	fixed_t xmove, ymove;
	fixed_t oldx, oldy; // reducing bobbing/momentum on ice when up against walls
	boolean moved;

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	moved = true;

	// if it's stopped
	if (!mo->momx && !mo->momy)
	{
		if (mo->flags2 & MF2_SKULLFLY)
		{
			// the skull slammed into something
			mo->flags2 &= ~MF2_SKULLFLY;
			mo->momx = mo->momy = mo->momz = 0;

			// set in 'search new direction' state?
			if (mo->type != MT_EGGMOBILE)
				P_SetMobjState(mo, mo->info->spawnstate);

			return;
		}
	}

	player = mo->player; //valid only if player avatar

	xmove = mo->momx;
	ymove = mo->momy;

	oldx = mo->x;
	oldy = mo->y;

	// Pushables can break some blocks
Alam Ed Arias's avatar
Alam Ed Arias committed
1154
	if (CheckForBustableBlocks && mo->flags & MF_PUSHABLE)
Alam Ed Arias's avatar
Alam Ed Arias committed
1155 1156 1157 1158 1159 1160
		P_PushableCheckBustables(mo);

	if (!P_TryMove(mo, mo->x + xmove, mo->y + ymove, true) && !tmsprung)
	{
		// blocked move

Alam Ed Arias's avatar
Alam Ed Arias committed
1161
		if (player) {
Alam Ed Arias's avatar
Alam Ed Arias committed
1162
			moved = false;
Alam Ed Arias's avatar
Alam Ed Arias committed
1163 1164
			if (player->bot)
				B_MoveBlocked(player);
Alam Ed Arias's avatar
Alam Ed Arias committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
		}

		if (mo->flags & MF_BOUNCE)
		{
			P_BounceMove(mo);
			xmove = ymove = 0;
			S_StartSound(mo, mo->info->activesound);

			// Bounce ring algorithm
			if (mo->type == MT_THROWNBOUNCE)
			{
				mo->threshold++;

				// Gain lower amounts of time on each bounce.
				if (mo->threshold < 5)
					mo->fuse += ((5 - mo->threshold) * TICRATE);

				// Check for hit against sky here
				if (P_CheckSkyHit(mo))
				{
					// Hack to prevent missiles exploding
					// against the sky.
					// Does not handle sky floors.
					// Check frontsector as well.

					P_RemoveMobj(mo);
					return;
				}
			}
		}
		else if (mo->flags & MF_STICKY)
		{
			S_StartSound(mo, mo->info->activesound);
			mo->momx = mo->momy = mo->momz = 0; //Full stop!
			mo->flags |= MF_NOGRAVITY; //Stay there!
			mo->flags &= ~MF_STICKY; //Don't check again!

			// Check for hit against sky here
			if (P_CheckSkyHit(mo))
			{
				// Hack to prevent missiles exploding
				// against the sky.
				// Does not handle sky floors.
				// Check frontsector as well.

				P_RemoveMobj(mo);
				return;
			}
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1214
		else if (player || mo->flags & (MF_SLIDEME|MF_PUSHABLE))
Alam Ed Arias's avatar
Alam Ed Arias committed
1215 1216 1217 1218
		{ // try to slide along it
			P_SlideMove(mo);
			xmove = ymove = 0;
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1219 1220 1221 1222 1223
		else if (mo->type == MT_SPINFIRE)
		{
			P_RemoveMobj(mo);
			return;
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
		else if (mo->flags & MF_MISSILE)
		{
			// explode a missile
			if (P_CheckSkyHit(mo))
			{
				// Hack to prevent missiles exploding
				// against the sky.
				// Does not handle sky floors.
				// Check frontsector as well.

				P_RemoveMobj(mo);
				return;
			}

			// draw damage on wall
			//SPLAT TEST ----------------------------------------------------------
#ifdef WALLSPLATS
			if (blockingline && mo->type != MT_REDRING && mo->type != MT_FIREBALL
Alam Ed Arias's avatar
Alam Ed Arias committed
1242
			&& !(mo->flags2 & (MF2_AUTOMATIC|MF2_RAILRING|MF2_BOUNCERING|MF2_EXPLOSION|MF2_SCATTER)))
Alam Ed Arias's avatar
Alam Ed Arias committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
				// set by last P_TryMove() that failed
			{
				divline_t divl;
				divline_t misl;
				fixed_t frac;

				P_MakeDivline(blockingline, &divl);
				misl.x = mo->x;
				misl.y = mo->y;
				misl.dx = mo->momx;
				misl.dy = mo->momy;
				frac = P_InterceptVector(&divl, &misl);
				R_AddWallSplat(blockingline, P_PointOnLineSide(mo->x,mo->y,blockingline),
					"A_DMG3", mo->z, frac, SPLATDRAWMODE_SHADE);
			}
#endif
			// --------------------------------------------------------- SPLAT TEST

			P_ExplodeMissile(mo);
			return;
		}
		else
			mo->momx = mo->momy = 0;
	}
Alam Ed Arias's avatar
Alam Ed Arias committed
1267
	else if (player)
Alam Ed Arias's avatar
Alam Ed Arias committed
1268 1269
		moved = true;

Alam Ed Arias's avatar
Alam Ed Arias committed
1270 1271 1272
	if (P_MobjWasRemoved(mo)) // MF_SPECIAL touched a player! O_o;;
		return;

Alam Ed Arias's avatar
Alam Ed Arias committed
1273 1274 1275
	// Check the gravity status.
	P_CheckGravity(mo, false);

Alam Ed Arias's avatar
Alam Ed Arias committed
1276
	if (player && !moved && player->pflags & PF_NIGHTSMODE && mo->target)
Alam Ed Arias's avatar
Alam Ed Arias committed
1277 1278 1279 1280
	{
		angle_t fa;

		P_UnsetThingPosition(mo);
Alam Ed Arias's avatar
Alam Ed Arias committed
1281 1282
		player->angle_pos = player->old_angle_pos;
		player->speed = FixedMul(player->speed, 4*FRACUNIT/5);
Alam Ed Arias's avatar
Alam Ed Arias committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
		if (player->flyangle >= 0 && player->flyangle < 90)
			player->flyangle = 135;
		else if (player->flyangle >= 90 && player->flyangle < 180)
			player->flyangle = 45;
		else if (player->flyangle >= 180 && player->flyangle < 270)
			player->flyangle = 315;
		else
			player->flyangle = 225;
		player->flyangle %= 360;

		if (player->pflags & PF_TRANSFERTOCLOSEST)
		{
			mo->x -= mo->momx;
			mo->y -= mo->momy;
		}
		else
		{
			fa = player->old_angle_pos>>ANGLETOFINESHIFT;

			mo->x = mo->target->x + FixedMul(FINECOSINE(fa),mo->target->radius);
			mo->y = mo->target->y + FixedMul(FINESINE(fa),mo->target->radius);
		}

		mo->momx = mo->momy = 0;
		P_SetThingPosition(mo);
	}

Alam Ed Arias's avatar
Alam Ed Arias committed
1310 1311 1312 1313
	if (mo->flags & MF_NOCLIPHEIGHT)
		return; // no frictions for objects that can pass through floors

	if (mo->flags & MF_MISSILE || mo->flags2 & MF2_SKULLFLY || mo->type == MT_SHELL || mo->type == MT_VULTURE)
Alam Ed Arias's avatar
Alam Ed Arias committed
1314 1315
		return; // no friction for missiles ever

Alam Ed Arias's avatar
Alam Ed Arias committed
1316
	if (player && player->homing) // no friction for homing
Alam Ed Arias's avatar
Alam Ed Arias committed
1317 1318 1319
		return;

	if (((!(mo->eflags & MFE_VERTICALFLIP) && mo->z > mo->floorz) || (mo->eflags & MFE_VERTICALFLIP && mo->z+mo->height < mo->ceilingz))
Alam Ed Arias's avatar
Alam Ed Arias committed
1320
		&& !(player && player->pflags & PF_SLIDING))
Alam Ed Arias's avatar
Alam Ed Arias committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
		return; // no friction when airborne

	P_XYFriction(mo, oldx, oldy);
}

static void P_RingXYMovement(mobj_t *mo)
{
	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	if (!P_SceneryTryMove(mo, mo->x + mo->momx, mo->y + mo->momy))
		P_SlideMove(mo);
}

static void P_SceneryXYMovement(mobj_t *mo)
{
	fixed_t oldx, oldy; // reducing bobbing/momentum on ice when up against walls

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	oldx = mo->x;
	oldy = mo->y;

	if (!P_SceneryTryMove(mo, mo->x + mo->momx, mo->y + mo->momy))
		P_SlideMove(mo);

Alam Ed Arias's avatar
Alam Ed Arias committed
1348
	if ((!(mo->eflags & MFE_VERTICALFLIP) && mo->z > mo->floorz) || (mo->eflags & MFE_VERTICALFLIP && mo->z+mo->height < mo->ceilingz))
Alam Ed Arias's avatar
Alam Ed Arias committed
1349 1350
		return; // no friction when airborne

Alam Ed Arias's avatar
Alam Ed Arias committed
1351 1352 1353
	if (mo->flags & MF_NOCLIPHEIGHT)
		return; // no frictions for objects that can pass through floors

Alam Ed Arias's avatar
Alam Ed Arias committed
1354 1355 1356
	P_SceneryXYFriction(mo, oldx, oldy);
}

Alam Ed Arias's avatar
Alam Ed Arias committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
//
// P_AdjustMobjFloorZ_FFloors
//
// Utility function for P_ZMovement and related
// Adjusts mo->floorz/mo->ceiling accordingly for FFloors
//
// "motype" determines what behaviour to use exactly
// This is to keep things consistent in case these various object types NEED to be different
//
// motype options:
// 0 - normal
// 1 - forces false check for water (rings)
// 2 - forces false check for water + different quicksand behaviour (scenery)
//
static void P_AdjustMobjFloorZ_FFloors(mobj_t *mo, sector_t *sector, UINT8 motype)
Alam Ed Arias's avatar
Alam Ed Arias committed
1372
{
Alam Ed Arias's avatar
Alam Ed Arias committed
1373 1374 1375
	ffloor_t *rover;
	fixed_t delta1, delta2, thingtop;

Alam Ed Arias's avatar
Alam Ed Arias committed
1376 1377 1378
	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

Alam Ed Arias's avatar
Alam Ed Arias committed
1379 1380 1381
	thingtop = mo->z + mo->height;

	for (rover = sector->ffloors; rover; rover = rover->next)
Alam Ed Arias's avatar
Alam Ed Arias committed
1382
	{
Alam Ed Arias's avatar
Alam Ed Arias committed
1383 1384
		if (!(rover->flags & FF_EXISTS))
			continue;
Alam Ed Arias's avatar
Alam Ed Arias committed
1385

Alam Ed Arias's avatar
Alam Ed Arias committed
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
		if (mo->player && (P_CheckSolidLava(mo, rover) || P_CanRunOnWater(mo->player, rover))) // only the player should be affected
			;
		else if (motype != 0 && rover->flags & FF_SWIMMABLE) // "scenery" only
			continue;
		else if (rover->flags & FF_QUICKSAND) // quicksand
			;
		else if (!((rover->flags & FF_BLOCKPLAYER && mo->player) // solid to players?
			    || (rover->flags & FF_BLOCKOTHERS && !mo->player))) // solid to others?
			continue;
		if (rover->flags & FF_QUICKSAND)
Alam Ed Arias's avatar
Alam Ed Arias committed
1396
		{
Alam Ed Arias's avatar
Alam Ed Arias committed
1397
			switch (motype)
Alam Ed Arias's avatar
Alam Ed Arias committed
1398
			{
Alam Ed Arias's avatar
Alam Ed Arias committed
1399 1400 1401
				case 2: // scenery does things differently for some reason
					if (mo->z < *rover->topheight && *rover->bottomheight < thingtop)
					{
Alam Ed Arias's avatar
Alam Ed Arias committed
1402
						mo->floorz = mo->z;
Alam Ed Arias's avatar
Alam Ed Arias committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
						continue;
					}
					break;
				default:
					if (mo->z < *rover->topheight && *rover->bottomheight < thingtop)
					{
						if (mo->floorz < mo->z)
							mo->floorz = mo->z;
					}
					continue; // This is so you can jump/spring up through quicksand from below.
Alam Ed Arias's avatar
Alam Ed Arias committed
1413
			}
Alam Ed Arias's avatar
Alam Ed Arias committed
1414
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1415

Alam Ed Arias's avatar
Alam Ed Arias committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
		delta1 = mo->z - (*rover->bottomheight + ((*rover->topheight - *rover->bottomheight)/2));
		delta2 = thingtop - (*rover->bottomheight + ((*rover->topheight - *rover->bottomheight)/2));
		if (*rover->topheight > mo->floorz && abs(delta1) < abs(delta2)
			&& !(rover->flags & FF_REVERSEPLATFORM))
		{
			mo->floorz = *rover->topheight;
		}
		if (*rover->bottomheight < mo->ceilingz && abs(delta1) >= abs(delta2)
			&& !(rover->flags & FF_PLATFORM))
		{
			mo->ceilingz = *rover->bottomheight;
		}
	}
}

//
// P_AdjustMobjFloorZ_PolyObjs
//
// Utility function for P_ZMovement and related
// Adjusts mo->floorz/mo->ceiling accordingly for PolyObjs
//
static void P_AdjustMobjFloorZ_PolyObjs(mobj_t *mo, subsector_t *subsec)
{
	polyobj_t *po = subsec->polyList;
	sector_t *polysec;
	fixed_t delta1, delta2, thingtop;
	fixed_t polytop, polybottom;

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	thingtop = mo->z + mo->height;

	while(po)
	{
		if (!P_MobjInsidePolyobj(po, mo) || !(po->flags & POF_SOLID))
		{
			po = (polyobj_t *)(po->link.next);
			continue;
		}

		// We're inside it! Yess...
		polysec = po->lines[0]->backsector;

		if (po->flags & POF_CLIPPLANES)
		{
			polytop = polysec->ceilingheight;
			polybottom = polysec->floorheight;
		}
		else
		{
			polytop = INT32_MAX;
			polybottom = INT32_MIN;
Alam Ed Arias's avatar
Alam Ed Arias committed
1469
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480

		delta1 = mo->z - (polybottom + ((polytop - polybottom)/2));
		delta2 = thingtop - (polybottom + ((polytop - polybottom)/2));

		if (polytop > mo->floorz && abs(delta1) < abs(delta2))
			mo->floorz = polytop;

		if (polybottom < mo->ceilingz && abs(delta1) >= abs(delta2))
			mo->ceilingz = polybottom;

		po = (polyobj_t *)(po->link.next);
Alam Ed Arias's avatar
Alam Ed Arias committed
1481
	}
Alam Ed Arias's avatar
Alam Ed Arias committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
}

static void P_RingZMovement(mobj_t *mo)
{
	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	// Intercept the stupid 'fall through 3dfloors' bug
	if (mo->subsector->sector->ffloors)
		P_AdjustMobjFloorZ_FFloors(mo, mo->subsector->sector, 1);
	if (mo->subsector->polyList)
		P_AdjustMobjFloorZ_PolyObjs(mo, mo->subsector);
Alam Ed Arias's avatar
Alam Ed Arias committed
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512

	// adjust height
	if (mo->pmomz && mo->z != mo->floorz)
	{
		mo->momz += mo->pmomz;
		mo->pmomz = 0;
	}
	mo->z += mo->momz;

	// clip movement
	if (mo->z <= mo->floorz && !(mo->flags & MF_NOCLIPHEIGHT))
	{
		mo->z = mo->floorz;

		mo->momz = 0;
	}
	else if (mo->z + mo->height > mo->ceilingz && !(mo->flags & MF_NOCLIPHEIGHT))
	{
		mo->z = mo->ceilingz - mo->height;
Alam Ed Arias's avatar
Alam Ed Arias committed
1513 1514

		mo->momz = 0;
Alam Ed Arias's avatar
Alam Ed Arias committed
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	}
}

boolean P_CheckDeathPitCollide(mobj_t *mo)
{
	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	if (((mo->z <= mo->subsector->sector->floorheight
		&& !(mo->eflags & MFE_VERTICALFLIP) && (mo->subsector->sector->flags & SF_FLIPSPECIAL_FLOOR))
	|| (mo->z + mo->height >= mo->subsector->sector->ceilingheight
		&& (mo->eflags & MFE_VERTICALFLIP) && (mo->subsector->sector->flags & SF_FLIPSPECIAL_CEILING)))
	&& (GETSECSPECIAL(mo->subsector->sector->special, 1) == 6
	|| GETSECSPECIAL(mo->subsector->sector->special, 1) == 7))
		return true;

	return false;
}

boolean P_CheckSolidLava(mobj_t *mo, ffloor_t *rover)
{
	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

Alam Ed Arias's avatar
Alam Ed Arias committed
1539
	if (rover->flags & FF_SWIMMABLE && GETSECSPECIAL(rover->master->frontsector->special, 1) == 3
Alam Ed Arias's avatar
Alam Ed Arias committed
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
		&& !(rover->master->flags & ML_BLOCKMONSTERS)
		&& ((rover->master->flags & ML_EFFECT3) || mo->z-mo->momz > *rover->topheight - FixedMul(16*FRACUNIT, mo->scale)))
			return true;

	return false;
}

//
// P_ZMovement
// Returns false if the mobj was killed/exploded/removed, true otherwise.
//
static boolean P_ZMovement(mobj_t *mo)
{
	fixed_t dist, delta;

	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	// Intercept the stupid 'fall through 3dfloors' bug
	if (mo->subsector->sector->ffloors)
Alam Ed Arias's avatar
Alam Ed Arias committed
1560 1561 1562
		P_AdjustMobjFloorZ_FFloors(mo, mo->subsector->sector, 0);
	if (mo->subsector->polyList)
		P_AdjustMobjFloorZ_PolyObjs(mo, mo->subsector);
Alam Ed Arias's avatar
Alam Ed Arias committed
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740

	// adjust height
	if (mo->pmomz && mo->z != mo->floorz)
	{
		mo->momz += mo->pmomz;
		mo->pmomz = 0;
	}
	mo->z += mo->momz;

	switch (mo->type)
	{
		case MT_THROWNBOUNCE:
			if ((mo->flags & MF_BOUNCE) && (mo->z <= mo->floorz || mo->z+mo->height >= mo->ceilingz))
			{
				mo->momz = -mo->momz;
				mo->z += mo->momz;
				S_StartSound(mo, mo->info->activesound);
				mo->threshold++;

				// Be sure to change the XY one too if you change this.
				// Gain lower amounts of time on each bounce.
				if (mo->threshold < 5)
					mo->fuse += ((5 - mo->threshold) * TICRATE);
			}
			break;

		case MT_SKIM:
			// skims don't bounce
			if (mo->z > mo->watertop && mo->z - mo->momz <= mo->watertop)
			{
				mo->z = mo->watertop;
				mo->momz = 0;
				mo->flags |= MF_NOGRAVITY;
			}
			break;
		case MT_GOOP:
			if (P_CheckDeathPitCollide(mo))
			{
				P_RemoveMobj(mo);
				return false;
			}
			if (mo->z <= mo->floorz && mo->momz)
			{
				P_SetMobjState(mo, mo->info->meleestate);
				mo->momx = mo->momy = mo->momz = 0;
				mo->z = mo->floorz;
				if (mo->info->painsound)
					S_StartSound(mo, mo->info->painsound);
			}
			break;
		case MT_FALLINGROCK:
		case MT_BIGTUMBLEWEED:
		case MT_LITTLETUMBLEWEED:
		case MT_SHELL:
			// Remove stuff from death pits.
			if (P_CheckDeathPitCollide(mo))
			{
				P_RemoveMobj(mo);
				return false;
			}
			break;
		case MT_REDFLAG:
		case MT_BLUEFLAG:
			// Remove from death pits.  DON'T FUCKING DESPAWN IT DAMMIT
			if (P_CheckDeathPitCollide(mo))
			{
				mo->fuse = 1;
				return false;
			}
			break;

		case MT_RING: // Ignore still rings
		case MT_COIN:
#ifdef BLUE_SPHERES
		case MT_BLUEBALL:
#endif
		case MT_REDTEAMRING:
		case MT_BLUETEAMRING:
		case MT_FLINGRING:
		case MT_FLINGCOIN:
			// Remove flinged stuff from death pits.
			if (P_CheckDeathPitCollide(mo))
			{
				P_RemoveMobj(mo);
				return false;
			}
			if (!(mo->momx || mo->momy || mo->momz))
				return true;
			break;
		case MT_BOUNCERING:
		case MT_INFINITYRING:
		case MT_AUTOMATICRING:
		case MT_RAILRING:
		case MT_EXPLOSIONRING:
		case MT_SCATTERRING:
		case MT_GRENADERING:
		case MT_BOUNCEPICKUP:
		case MT_RAILPICKUP:
		case MT_AUTOPICKUP:
		case MT_EXPLODEPICKUP:
		case MT_SCATTERPICKUP:
		case MT_GRENADEPICKUP:
			// Remove flinged stuff from death pits.
			if (P_CheckDeathPitCollide(mo) && (mo->flags2 & MF2_DONTRESPAWN))
			{
				P_RemoveMobj(mo);
				return false;
			}
			if (!(mo->momx || mo->momy || mo->momz))
				return true;
			break;
		case MT_FLINGEMERALD:
		case MT_NIGHTSWING:
			if (!(mo->momx || mo->momy || mo->momz))
				return true;
			break;
		case MT_FLAMEJET:
		case MT_VERTICALFLAMEJET:
			if (!(mo->flags & MF_BOUNCE))
				return true;
			break;
		case MT_SPIKE:
			// Dead spike particles disappear upon ground contact
			if ((mo->z <= mo->floorz || mo->z + mo->height >= mo->ceilingz) && mo->health <= 0)
			{
				P_RemoveMobj(mo);
				return false;
			}
			break;
		default:
			break;
	}

	if (P_CheckDeathPitCollide(mo))
	{
		if (mo->flags & MF_PUSHABLE)
		{
			// Remove other pushable items from death pits.
			P_RemoveMobj(mo);
			return false;
		}
		else if (mo->flags & MF_ENEMY || mo->flags & MF_BOSS)
		{
			// Kill enemies and bosses that fall into death pits.
			if (mo->health)
			{
				P_KillMobj(mo, NULL, NULL);
				return false;
			}
		}
	}

	if (P_MobjFlip(mo)*mo->momz < 0
	&& (mo->flags2 & MF2_CLASSICPUSH))
		mo->momx = mo->momy = 0;

	if (mo->flags & MF_FLOAT && mo->target && mo->health
	&& !(mo->type == MT_EGGMOBILE) && mo->target->health > 0)
	{
		// float down towards target if too close
		if (!(mo->flags2 & MF2_SKULLFLY) && !(mo->flags2 & MF2_INFLOAT))
		{
			dist = P_AproxDistance(mo->x - mo->target->x, mo->y - mo->target->y);

			delta = (mo->target->z + (mo->height>>1)) - mo->z;

			if (delta < 0 && dist < -(delta*3))
				mo->z -= FixedMul(FLOATSPEED, mo->scale);
			else if (delta > 0 && dist < (delta*3))
				mo->z += FixedMul(FLOATSPEED, mo->scale);

			if (mo->type == MT_JETJAW && mo->z + mo->height > mo->watertop)
				mo->z = mo->watertop - mo->height;
		}

	}

	// clip movement
Alam Ed Arias's avatar
Alam Ed Arias committed
1741 1742 1743
	if (((mo->z <= mo->floorz && !(mo->eflags & MFE_VERTICALFLIP))
		|| (mo->z + mo->height >= mo->ceilingz && mo->eflags & MFE_VERTICALFLIP))
	&& !(mo->flags & MF_NOCLIPHEIGHT))
Alam Ed Arias's avatar
Alam Ed Arias committed
1744
	{
Alam Ed Arias's avatar
Alam Ed Arias committed
1745 1746 1747 1748 1749
		if (mo->eflags & MFE_VERTICALFLIP)
			mo->z = mo->ceilingz - mo->height;
		else
			mo->z = mo->floorz;

Alam Ed Arias's avatar
Alam Ed Arias committed
1750 1751
		// hit the floor
		if (mo->type == MT_FIREBALL) // special case for the fireball
Alam Ed Arias's avatar
Alam Ed Arias committed
1752 1753 1754
			mo->momz = P_MobjFlip(mo)*FixedMul(5*FRACUNIT, mo->scale);
		else if (mo->type == MT_SPINFIRE) // elemental shield fire is another exception here
			;
Alam Ed Arias's avatar
Alam Ed Arias committed
1755 1756 1757 1758 1759 1760 1761 1762 1763
		else if (mo->flags & MF_MISSILE)
		{
			if (!(mo->flags & MF_NOCLIP))
			{
				// This is a really ugly hard-coded hack to prevent grenades
				// from exploding the instant they hit the ground, and then
				// another to prevent them from turning into hockey pucks.
				// I'm sorry in advance. -SH
				// PS: Oh, and Brak's napalm bombs too, now.
Alam Ed Arias's avatar
Alam Ed Arias committed
1764
				if (mo->flags & MF_GRENADEBOUNCE)
Alam Ed Arias's avatar
Alam Ed Arias committed
1765
				{
Alam Ed Arias's avatar
Alam Ed Arias committed
1766
					// Going down? (Or up in reverse gravity?)
Alam Ed Arias's avatar
Alam Ed Arias committed
1767
					if (P_MobjFlip(mo)*mo->momz < 0)
Alam Ed Arias's avatar
Alam Ed Arias committed
1768
					{
Alam Ed Arias's avatar
Alam Ed Arias committed
1769 1770
						// If going slower than a fracunit, just stop.
						if (abs(mo->momz) < FixedMul(FRACUNIT, mo->scale))
Alam Ed Arias's avatar
Alam Ed Arias committed
1771 1772
						{
							mo->momx = mo->momy = mo->momz = 0;
Alam Ed Arias's avatar
Alam Ed Arias committed
1773 1774 1775 1776

							// Napalm hack
							if (mo->type == MT_CYBRAKDEMON_NAPALM_BOMB_LARGE && mo->fuse)
								mo->fuse = 1;
Alam Ed Arias's avatar
Alam Ed Arias committed
1777
						}
Alam Ed Arias's avatar
Alam Ed Arias committed
1778
						// Otherwise bounce up at half speed.
Alam Ed Arias's avatar
Alam Ed Arias committed
1779
						else
Alam Ed Arias's avatar
Alam Ed Arias committed
1780
							mo->momz = -mo->momz/2;
Alam Ed Arias's avatar
Alam Ed Arias committed
1781 1782 1783 1784 1785 1786 1787
						S_StartSound(mo, mo->info->activesound);
					}
				}
				// Hack over. Back to your regularly scheduled detonation. -SH
				else
				{
					// Don't explode on the sky!
Alam Ed Arias's avatar
Alam Ed Arias committed
1788 1789
					if (!(mo->eflags & MFE_VERTICALFLIP)
					&& mo->subsector->sector->floorpic == skyflatnum
Alam Ed Arias's avatar
Alam Ed Arias committed
1790 1791
					&& mo->subsector->sector->floorheight == mo->floorz)
						P_RemoveMobj(mo);
Alam Ed Arias's avatar
Alam Ed Arias committed
1792 1793 1794 1795
					else if (mo->eflags & MFE_VERTICALFLIP
					&& mo->subsector->sector->ceilingpic == skyflatnum
					&& mo->subsector->sector->ceilingheight == mo->ceilingz)
						P_RemoveMobj(mo);
Alam Ed Arias's avatar
Alam Ed Arias committed
1796 1797 1798 1799 1800 1801 1802
					else
						P_ExplodeMissile(mo);
					return false;
				}
			}
		}

Alam Ed Arias's avatar
Alam Ed Arias committed
1803
		if (P_MobjFlip(mo)*mo->momz < 0) // falling
Alam Ed Arias's avatar
Alam Ed Arias committed
1804
		{
Alam Ed Arias's avatar
Alam Ed Arias committed
1805 1806 1807
			if (!tmfloorthing || tmfloorthing->flags & (MF_PUSHABLE|MF_MONITOR)
			|| tmfloorthing->flags2 & MF2_STANDONME || tmfloorthing->type == MT_PLAYER)
				mo->eflags |= MFE_JUSTHITFLOOR;
Alam Ed Arias's avatar
Alam Ed Arias committed
1808

Alam Ed Arias's avatar
Alam Ed Arias committed
1809 1810 1811
			if (mo->flags2 & MF2_SKULLFLY) // the skull slammed into something
				mo->momz = -mo->momz;
			else
Alam Ed Arias's avatar
Alam Ed Arias committed
1812 1813 1814 1815 1816 1817 1818 1819
			// Flingrings bounce
			if (mo->type == MT_FLINGRING
				|| mo->type == MT_FLINGCOIN
				|| P_WeaponOrPanel(mo->type)
				|| mo->type == MT_FLINGEMERALD
				|| mo->type == MT_BIGTUMBLEWEED
				|| mo->type == MT_LITTLETUMBLEWEED
				|| mo->type == MT_CANNONBALLDECOR
Alam Ed Arias's avatar
Alam Ed Arias committed
1820
				|| mo->type == MT_FALLINGROCK)
Alam Ed Arias's avatar
Alam Ed Arias committed
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
			{
				if (maptol & TOL_NIGHTS)
					mo->momz = -FixedDiv(mo->momz, 10*FRACUNIT);
				else
					mo->momz = -FixedMul(mo->momz, FixedDiv(17*FRACUNIT,20*FRACUNIT));

				if (mo->type == MT_BIGTUMBLEWEED || mo->type == MT_LITTLETUMBLEWEED)
				{
					if (abs(mo->momx) < FixedMul(STOPSPEED, mo->scale)
						&& abs(mo->momy) < FixedMul(STOPSPEED, mo->scale)
						&& abs(mo->momz) < FixedMul(STOPSPEED*3, mo->scale))
					{
						if (!(mo->flags & MF_AMBUSH))
						{
							mo->momx = mo->momy = mo->momz = 0;
							P_SetMobjState(mo, mo->info->spawnstate);
						}
						else
						{
							// If deafed, give the tumbleweed another random kick if it runs out of steam.
Alam Ed Arias's avatar
Alam Ed Arias committed
1841
							mo->momz += P_MobjFlip(mo)*FixedMul(6*FRACUNIT, mo->scale);
Alam Ed Arias's avatar
Alam Ed Arias committed
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860

							if (P_Random() & 1)
								mo->momx += FixedMul(6*FRACUNIT, mo->scale);
							else
								mo->momx -= FixedMul(6*FRACUNIT, mo->scale);

							if (P_Random() & 1)
								mo->momy += FixedMul(6*FRACUNIT, mo->scale);
							else
								mo->momy -= FixedMul(6*FRACUNIT, mo->scale);
						}
					}

					// Stolen from P_SpawnFriction
					mo->friction = FRACUNIT - 0x100;
					mo->movefactor = ((0x10092 - mo->friction)*(0x70))/0x158;
				}
				else if (mo->type == MT_FALLINGROCK)
				{
Alam Ed Arias's avatar
Alam Ed Arias committed
1861
					if (P_MobjFlip(mo)*mo->momz > FixedMul(2*FRACUNIT, mo->scale))
Alam Ed Arias's avatar
Alam Ed Arias committed
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
						S_StartSound(mo, mo->info->activesound + P_RandomKey(mo->info->mass));

					mo->momz /= 2; // Rocks not so bouncy

					if (abs(mo->momx) < FixedMul(STOPSPEED, mo->scale)
						&& abs(mo->momy) < FixedMul(STOPSPEED, mo->scale)
						&& abs(mo->momz) < FixedMul(STOPSPEED*3, mo->scale))
					{
						P_RemoveMobj(mo);
						return false;
					}
				}
				else if (mo->type == MT_CANNONBALLDECOR)
				{
					mo->momz /= 2;
					if (abs(mo->momz) < FixedMul(STOPSPEED*3, mo->scale))
						mo->momz = 0;
				}
			}
Alam Ed Arias's avatar
Alam Ed Arias committed
1881
			else if (tmfloorthing && (tmfloorthing->flags & (MF_PUSHABLE|MF_MONITOR)
Alam Ed Arias's avatar
Alam Ed Arias committed
1882 1883 1884 1885 1886
			|| tmfloorthing->flags2 & MF2_STANDONME || tmfloorthing->type == MT_PLAYER))
				mo->momz = tmfloorthing->momz;
			else if (!tmfloorthing)
				mo->momz = 0;
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1887 1888 1889
		else if (tmfloorthing && (tmfloorthing->flags & (MF_PUSHABLE|MF_MONITOR)
		|| tmfloorthing->flags2 & MF2_STANDONME || tmfloorthing->type == MT_PLAYER))
			mo->momz = tmfloorthing->momz;
Alam Ed Arias's avatar
Alam Ed Arias committed
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901

		if (mo->type == MT_STEAM)
			return true;
	}
	else if (!(mo->flags & MF_NOGRAVITY)) // Gravity here!
	{
		/// \todo may not be needed (done in P_MobjThinker normally)
		mo->eflags &= ~MFE_JUSTHITFLOOR;

		P_CheckGravity(mo, true);
	}

Alam Ed Arias's avatar
Alam Ed Arias committed
1902 1903 1904
	if (((mo->z + mo->height > mo->ceilingz && !(mo->eflags & MFE_VERTICALFLIP))
		|| (mo->z < mo->floorz && mo->eflags & MFE_VERTICALFLIP))
	&& !(mo->flags & MF_NOCLIPHEIGHT))
Alam Ed Arias's avatar
Alam Ed Arias committed
1905
	{
Alam Ed Arias's avatar
Alam Ed Arias committed
1906 1907 1908 1909
		if (mo->eflags & MFE_VERTICALFLIP)
			mo->z = mo->floorz;
		else
			mo->z = mo->ceilingz - mo->height;
Alam Ed Arias's avatar
Alam Ed Arias committed
1910

Alam Ed Arias's avatar
Alam Ed Arias committed
1911 1912 1913
		if (mo->type == MT_SPINFIRE)
			;
		else if ((mo->flags & MF_MISSILE) && !(mo->flags & MF_NOCLIP))
Alam Ed Arias's avatar
Alam Ed Arias committed
1914 1915
		{
			// Hack 2: Electric Boogaloo -SH
Alam Ed Arias's avatar
Alam Ed Arias committed
1916
			if (mo->flags & MF_GRENADEBOUNCE)
Alam Ed Arias's avatar
Alam Ed Arias committed
1917
			{
Alam Ed Arias's avatar
Alam Ed Arias committed
1918
				if (P_MobjFlip(mo)*mo->momz >= 0)
Alam Ed Arias's avatar
Alam Ed Arias committed
1919 1920 1921 1922 1923 1924 1925 1926
				{
					mo->momz = -mo->momz;
					S_StartSound(mo, mo->info->activesound);
				}
			}
			else
			{
				// Don't explode on the sky!
Alam Ed Arias's avatar
Alam Ed Arias committed
1927 1928
				if (!(mo->eflags & MFE_VERTICALFLIP)
				&& mo->subsector->sector->ceilingpic == skyflatnum
Alam Ed Arias's avatar
Alam Ed Arias committed
1929 1930
				&& mo->subsector->sector->ceilingheight == mo->ceilingz)
					P_RemoveMobj(mo);
Alam Ed Arias's avatar
Alam Ed Arias committed
1931 1932 1933 1934
				else if (mo->eflags & MFE_VERTICALFLIP
				&& mo->subsector->sector->floorpic == skyflatnum
				&& mo->subsector->sector->floorheight == mo->floorz)
					P_RemoveMobj(mo);
Alam Ed Arias's avatar
Alam Ed Arias committed
1935 1936 1937 1938 1939
				else
					P_ExplodeMissile(mo);
				return false;
			}
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956

		if (P_MobjFlip(mo)*mo->momz > 0) // hit the ceiling
		{
			if (mo->flags2 & MF2_SKULLFLY) // the skull slammed into something
				mo->momz = -mo->momz;
			else
			// Flags bounce
			if (mo->type == MT_REDFLAG || mo->type == MT_BLUEFLAG)
			{
				if (maptol & TOL_NIGHTS)
					mo->momz = -FixedDiv(mo->momz, 10*FRACUNIT);
				else
					mo->momz = -FixedMul(mo->momz, FixedDiv(17*FRACUNIT,20*FRACUNIT));
			}
			else
				mo->momz = 0;
		}
Alam Ed Arias's avatar
Alam Ed Arias committed
1957
	}
Alam Ed Arias's avatar
Alam Ed Arias committed
1958

Alam Ed Arias's avatar
Alam Ed Arias committed
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
	return true;
}

static void P_PlayerZMovement(mobj_t *mo)
{
	I_Assert(mo != NULL);
	I_Assert(!P_MobjWasRemoved(mo));

	if (!mo->player)
		return;

	// Intercept the stupid 'fall through 3dfloors' bug
	if (mo->subsector->sector->ffloors)
Alam Ed Arias's avatar
Alam Ed Arias committed
1972 1973 1974
		P_AdjustMobjFloorZ_FFloors(mo, mo->subsector->sector, 0);
	if (mo->subsector->polyList)
		P_AdjustMobjFloorZ_PolyObjs(mo, mo->subsector);
Alam Ed Arias's avatar
Alam Ed Arias committed
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003

	// check for smooth step up
	if ((mo->eflags & MFE_VERTICALFLIP && mo->z + mo->height > mo->ceilingz)
		|| (!(mo->eflags & MFE_VERTICALFLIP) && mo->z < mo->floorz))
	{
		if (mo->eflags & MFE_VERTICALFLIP)
			mo->player->viewheight -= (mo->z+mo->height) - mo->ceilingz;
		else
			mo->player->viewheight -= mo->floorz - mo->z;

		mo->player->deltaviewheight =
			(FixedMul(cv_viewheight.value<<FRACBITS, mo->scale) - mo->player->viewheight)>>3;
	}

	// adjust height
/*	if (mo->pmomz && mo->z > mo->floorz && !(mo->player->pflags & PF_JUMPED))
	{
		mo->momz += mo->pmomz;
		mo->pmomz = 0;
	}*/

	mo->z += mo->momz;

	// Have player fall through floor?
	if (mo->player->playerstate == PST_DEAD
	|| mo->player->playerstate == PST_REBORN)
		return;

	// clip movement
Alam Ed Arias's avatar
Alam Ed Arias committed
2004
	if (P_IsObjectOnGround(mo) && !(mo->flags & MF_NOCLIPHEIGHT))
Alam Ed Arias's avatar
Alam Ed Arias committed
2005
	{
Alam Ed Arias's avatar
Alam Ed Arias committed
2006 2007 2008 2009 2010
		if (mo->eflags & MFE_VERTICALFLIP)
			mo->z = mo->ceilingz - mo->height;
		else
			mo->z = mo->floorz;

Alam Ed Arias's avatar
Alam Ed Arias committed
2011 2012 2013
		if (mo->player && (mo->player->pflags & PF_NIGHTSMODE))
		{
			if (mo->player->flyangle < 90 || mo->player->flyangle >= 270)
Alam Ed Arias's avatar
Alam Ed Arias committed
2014
				mo->player->flyangle += P_MobjFlip(mo)*90;
Alam Ed Arias's avatar
Alam Ed Arias committed
2015
			else
Alam Ed Arias's avatar
Alam Ed Arias committed
2016
				mo->player->flyangle -= P_MobjFlip(mo)*90;
Alam Ed Arias's avatar
Alam Ed Arias committed
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
			mo->player->speed = FixedMul(mo->player->speed, 4*FRACUNIT/5);
			goto nightsdone;
		}
		// Get up if you fell.
		if (mo->state == &states[mo->info->painstate] || mo->state == &states[S_PLAY_SUPERHIT])
			P_SetPlayerMobjState(mo, S_PLAY_STND);

		if (P_MobjFlip(mo)*mo->momz < 0) // falling
		{
			// Squat down. Decrease viewheight for a moment after hitting the ground (hard),
			if (mo->player)
			{
				if (P_MobjFlip(mo)*mo->momz < -FixedMul(8*FRACUNIT, mo->scale))
					mo->player->deltaviewheight = (P_MobjFlip(mo)*mo->momz)>>3; // make sure momz is negative
			}

Alam Ed Arias's avatar
Alam Ed Arias committed
2033 2034
			if (!tmfloorthing || tmfloorthing->flags & (MF_PUSHABLE|MF_MONITOR)
				|| tmfloorthing->flags2 & MF2_STANDONME || tmfloorthing->type == MT_PLAYER) // Spin Attack
Alam Ed Arias's avatar
Alam Ed Arias committed
2035
			{
Alam Ed Arias's avatar
Alam Ed Arias committed
2036
				mo->eflags |= MFE_JUSTHITFLOOR; // Spin Attack
Alam Ed Arias's avatar
Alam Ed Arias committed
2037 2038 2039 2040 2041 2042 2043

				if (mo->eflags & MFE_JUSTHITFLOOR)
				{
#ifdef POLYOBJECTS
					// Check if we're on a polyobject
					// that triggers a linedef executor.
					msecnode_t *node;
Alam Ed Arias's avatar
Alam Ed Arias committed
2044
					boolean stopmovecut = false;
Alam Ed Arias's avatar
Alam Ed Arias committed
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065

					for (node = mo->touching_sectorlist; node; node = node->m_snext)
					{
						sector_t *sec = node->m_sector;
						subsector_t *newsubsec;
						size_t i;

						for (i = 0; i < numsubsectors; i++)
						{
							newsubsec = &subsectors[i];

							if (newsubsec->sector != sec)
								continue;

							if (newsubsec->polyList)
							{
								polyobj_t *po = newsubsec->polyList;
								sector_t *polysec;

								while(po)
								{
Alam Ed Arias's avatar
Alam Ed Arias committed
2066
									if (!P_MobjInsidePolyobj(po, mo) || !(po->flags & POF_SOLID))
Alam Ed Arias's avatar
Alam Ed Arias committed
2067 2068 2069 2070 2071
									{
										po = (polyobj_t *)(po->link.next);
										continue;
									}

Alam Ed Arias's avatar
Alam Ed Arias committed
2072
									// We're inside it! Yess...
Alam Ed Arias's avatar
Alam Ed Arias committed
2073 2074 2075
									polysec = po->lines[0]->backsector;

									// Moving polyobjects should act like conveyors if the player lands on one. (I.E. none of the momentum cut thing below) -Red
Alam Ed Arias's avatar
Alam Ed Arias committed
2076
									if ((mo->z == polysec->ceilingheight || mo->z+mo->height == polysec->floorheight) && po->thinker)
Alam Ed Arias's avatar
Alam Ed Arias committed
2077 2078
										stopmovecut = true;

Alam Ed Arias's avatar
Alam Ed Arias committed
2079
									if (!(po->flags & POF_LDEXEC))
Alam Ed Arias's avatar
Alam Ed Arias committed
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
									{
										po = (polyobj_t *)(po->link.next);
										continue;
									}

									if (mo->z == polysec->ceilingheight)
									{
										// We're landing on a PO, so check for
										// a linedef executor.
										// Trigger tags are 32000 + the PO's ID number.
										P_LinedefExecute((INT16)(32000 + po->id), mo, NULL);
									}

									po = (polyobj_t *)(po->link.next);
								}
							}
						}
					}
Alam Ed Arias's avatar
Alam Ed Arias committed
2098 2099

					if (!stopmovecut)
Alam Ed Arias's avatar
Alam Ed Arias committed
2100 2101 2102 2103
#endif

					// Cut momentum in half when you hit the ground and
					// aren't pressing any controls.
Alam Ed Arias's avatar
Alam Ed Arias committed
2104
					if (!mo->player || (!(mo->player->cmd.forwardmove || mo->player->cmd.sidemove) && !mo->player->cmomx && !mo->player->cmomy && !(mo->player->pflags & PF_SPINNING)))
Alam Ed Arias's avatar
Alam Ed Arias committed
2105 2106 2107 2108 2109 2110
					{
						mo->momx = mo->momx/2;
						mo->momy = mo->momy/2;
					}
				}

Alam Ed Arias's avatar
Alam Ed Arias committed
2111
				if (mo->health && mo->player)
Alam Ed Arias's avatar
Alam Ed Arias committed
2112 2113 2114 2115 2116 2117
				{
					if (mo->player->pflags & PF_GLIDING) // ground gliding
					{
						mo->player->skidtime = TICRATE;
						mo->tics = -1;
					}
Alam Ed Arias's avatar
Alam Ed Arias committed
2118 2119
					else if (mo->player->pflags & PF_JUMPED || (mo->player->pflags & (PF_SPINNING|PF_USEDOWN)) != (PF_SPINNING|PF_USEDOWN)
					|| mo->player->powers[pw_tailsfly] || (mo->state >= &states[S_PLAY_SPC1] && mo->state <= &states[S_PLAY_SPC4]))