p_mobj.c 375 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 16 17 18 19 20 21
//
// 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"
22
#include "r_skins.h"
Alam Ed Arias committed
23 24 25 26 27 28 29 30 31 32 33
#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"
34
#include "p_slopes.h"
35
#include "f_finale.h"
36
#include "m_cond.h"
Alam Ed Arias committed
37

Sal committed
38
static CV_PossibleValue_t CV_BobSpeed[] = {{0, "MIN"}, {4*FRACUNIT, "MAX"}, {0, NULL}};
39
consvar_t cv_movebob = CVAR_INIT ("movebob", "1.0", CV_FLOAT|CV_SAVE, CV_BobSpeed, NULL);
Sal committed
40

Alam Ed Arias committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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;
		astate = &states[ac->statenum];
Alam Ed Arias committed
60
		if (ac->mobj && !P_MobjWasRemoved(ac->mobj)) // just in case...
Alam Ed Arias committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			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;
}

78 79 80 81 82
//
// P_SetupStateAnimation
//
FUNCINLINE static ATTRINLINE void P_SetupStateAnimation(mobj_t *mobj, state_t *st)
{
83
	INT32 animlength = (mobj->sprite == SPR_PLAY && mobj->skin)
84 85 86
		? (INT32)(((skin_t *)mobj->skin)->sprites[mobj->sprite2].numframes) - 1
		: st->var1;

87 88
	if (!(st->frame & FF_ANIMATE))
		return;
89 90

	if (animlength <= 0 || st->var2 == 0)
91 92 93 94 95 96 97 98 99 100 101 102 103
	{
		mobj->frame &= ~FF_ANIMATE;
		return; // Crash/stupidity prevention
	}

	mobj->anim_duration = (UINT16)st->var2;

	if (st->frame & FF_GLOBALANIM)
	{
		// Attempt to account for the pre-ticker for objects spawned on load
		if (!leveltime) return;

		mobj->anim_duration -= (leveltime + 2) % st->var2;            // Duration synced to timer
104
		mobj->frame += ((leveltime + 2) / st->var2) % (animlength + 1); // Frame synced to timer (duration taken into account)
105 106 107
	}
	else if (st->frame & FF_RANDOMANIM)
	{
108
		mobj->frame += P_RandomKey(animlength + 1);     // Random starting frame
109 110 111 112
		mobj->anim_duration -= P_RandomKey(st->var2); // Random duration for first frame
	}
}

113 114 115 116 117 118 119 120
//
// P_CycleStateAnimation
//
FUNCINLINE static ATTRINLINE void P_CycleStateAnimation(mobj_t *mobj)
{
	// var2 determines delay between animation frames
	if (!(mobj->frame & FF_ANIMATE) || --mobj->anim_duration != 0)
		return;
121

122 123
	mobj->anim_duration = (UINT16)mobj->state->var2;

124
	if (mobj->sprite != SPR_PLAY)
125
	{
126 127 128 129 130 131
		// compare the current sprite frame to the one we started from
		// if more than var1 away from it, swap back to the original
		// else just advance by one
		if (((++mobj->frame) & FF_FRAMEMASK) - (mobj->state->frame & FF_FRAMEMASK) > (UINT32)mobj->state->var1)
			mobj->frame = (mobj->state->frame & FF_FRAMEMASK) | (mobj->frame & ~FF_FRAMEMASK);

132 133 134
		return;
	}

135 136 137
	// sprite2 version of above
	if (mobj->skin && (((++mobj->frame) & FF_FRAMEMASK) >= (UINT32)(((skin_t *)mobj->skin)->sprites[mobj->sprite2].numframes)))
		mobj->frame &= ~FF_FRAMEMASK;
138 139
}

Alam Ed Arias committed
140 141 142 143 144
//
// P_CycleMobjState
//
static void P_CycleMobjState(mobj_t *mobj)
{
145 146 147
	// state animations
	P_CycleStateAnimation(mobj);

Alam Ed Arias committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	// 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)
{
166 167 168
	// state animations
	P_CycleStateAnimation(mobj);

Alam Ed Arias committed
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
	// 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

205
	// Catch falling for nojumpspin
206
	if ((state == S_PLAY_JUMP) && (player->charflags & SF_NOJUMPSPIN) && (P_MobjFlip(mobj)*mobj->momz < 0))
207 208
		return P_SetPlayerMobjState(mobj, S_PLAY_FALL);

209
	// Catch swimming versus flying
lachwright committed
210 211
	if ((state == S_PLAY_FLY || (state == S_PLAY_GLIDE && skins[player->skin].sprites[SPR2_SWIM].numframes))
	&& player->mo->eflags & MFE_UNDERWATER && !player->skidtime)
212 213
		return P_SetPlayerMobjState(player->mo, S_PLAY_SWIM);
	else if (state == S_PLAY_SWIM && !(player->mo->eflags & MFE_UNDERWATER))
214 215 216 217 218 219
	{
		if (player->charability == CA_GLIDEANDCLIMB)
			return P_SetPlayerMobjState(player->mo, S_PLAY_GLIDE);
		else
			return P_SetPlayerMobjState(player->mo, S_PLAY_FLY);
	}
220

221
	// Catch SF_NOSUPERSPIN jumps for Supers
222
	if (player->powers[pw_super] && (player->charflags & SF_NOSUPERSPIN))
Alam Ed Arias committed
223
	{
224 225 226 227 228
		if (state == S_PLAY_JUMP)
		{
			if (player->mo->state-states == S_PLAY_WALK)
				return P_SetPlayerMobjState(mobj, S_PLAY_FLOAT);
			return true;
Alam Ed Arias committed
229
		}
230
		else if (player->mo->state-states == S_PLAY_FLOAT && state == S_PLAY_STND)
231
			return true;
Alam Ed Arias committed
232 233 234 235 236 237 238 239 240 241 242
	}
	// 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.
243 244 245 246
	switch(state)
	{
	case S_PLAY_STND:
	case S_PLAY_WAIT:
247
	case S_PLAY_NIGHTS_STAND:
Alam Ed Arias committed
248
		player->panim = PA_IDLE;
249
		break;
250 251 252
	case S_PLAY_EDGE:
		player->panim = PA_EDGE;
		break;
253
	case S_PLAY_WALK:
254
	case S_PLAY_SKID:
255
	case S_PLAY_FLOAT:
Alam Ed Arias committed
256
		player->panim = PA_WALK;
257 258
		break;
	case S_PLAY_RUN:
259
	case S_PLAY_FLOAT_RUN:
Alam Ed Arias committed
260
		player->panim = PA_RUN;
261
		break;
262 263
	case S_PLAY_DASH:
		player->panim = PA_DASH;
264
		break;
265
	case S_PLAY_PAIN:
266
	case S_PLAY_STUN:
267 268
		player->panim = PA_PAIN;
		break;
269 270
	case S_PLAY_ROLL:
	//case S_PLAY_SPINDASH: -- everyone can ROLL thanks to zoom tubes...
271
	case S_PLAY_NIGHTS_ATTACK:
Alam Ed Arias committed
272
		player->panim = PA_ROLL;
273
		break;
274 275 276
	case S_PLAY_JUMP:
		player->panim = PA_JUMP;
		break;
277 278
	case S_PLAY_SPRING:
		player->panim = PA_SPRING;
Yukita Mayako committed
279
		break;
280
	case S_PLAY_FALL:
281
	case S_PLAY_NIGHTS_FLOAT:
Alam Ed Arias committed
282
		player->panim = PA_FALL;
283 284
		break;
	case S_PLAY_FLY:
285
	case S_PLAY_FLY_TIRED:
286
	case S_PLAY_SWIM:
287
	case S_PLAY_GLIDE:
288 289
	case S_PLAY_BOUNCE:
	case S_PLAY_BOUNCE_LANDING:
290
	case S_PLAY_TWINSPIN:
Alam Ed Arias committed
291
		player->panim = PA_ABILITY;
292
		break;
293
	case S_PLAY_SPINDASH: // ...but the act of SPINDASHING is charability2 specific.
toaster committed
294
	case S_PLAY_FIRE:
toaster committed
295
	case S_PLAY_FIRE_FINISH:
toaster committed
296 297
	case S_PLAY_MELEE:
	case S_PLAY_MELEE_FINISH:
toaster committed
298
	case S_PLAY_MELEE_LANDING:
toaster committed
299 300
		player->panim = PA_ABILITY2;
		break;
Yukita Mayako committed
301 302 303
	case S_PLAY_RIDE:
		player->panim = PA_RIDE;
		break;
304
	default:
Alam Ed Arias committed
305
		player->panim = PA_ETC;
306 307
		break;
	}
Alam Ed Arias committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

	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.
328 329 330
		if (state == S_PLAY_STND && player->powers[pw_super] && skins[player->skin].sprites[SPR2_WAIT|FF_SPR2SUPER].numframes == 0) // if no super wait, don't wait at all
			mobj->tics = -1;
		else if (player->panim == PA_EDGE && (player->charflags & SF_FASTEDGE))
331 332
			mobj->tics = 2;
		else if (!(disableSpeedAdjust || player->charflags & SF_NOSPEEDADJUST))
Alam Ed Arias committed
333
		{
334
			fixed_t speed;// = FixedDiv(player->speed, FixedMul(mobj->scale, player->mo->movefactor));
335
			if (player->panim == PA_FALL)
Alam Ed Arias committed
336 337 338 339 340 341 342 343 344 345 346
			{
				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;
			}
347
			else if (player->panim == PA_ABILITY2 && player->charability2 == CA2_SPINDASH)
Alam Ed Arias committed
348
			{
349 350 351
				fixed_t step = (player->maxdash - player->mindash)/4;
				speed = (player->dashspeed - player->mindash);
				if (speed > 3*step)
352
					mobj->tics = 1;
353
				else if (speed > step)
354 355 356 357 358 359
					mobj->tics = 2;
				else
					mobj->tics = 3;
			}
			else
			{
360
				speed = FixedDiv(player->speed, FixedMul(mobj->scale, player->mo->movefactor));
361
				if (player->panim == PA_ROLL || player->panim == PA_JUMP)
Alam Ed Arias committed
362
				{
363
					if (speed > 16<<FRACBITS)
Alam Ed Arias committed
364 365 366 367
						mobj->tics = 1;
					else
						mobj->tics = 2;
				}
368
				else if (P_IsObjectOnGround(mobj) || ((player->charability == CA_FLOAT || player->charability == CA_SLOWFALL) && player->secondjump == 1) || player->powers[pw_super]) // Only if on the ground or superflying.
369 370 371 372 373 374 375 376 377 378
				{
					if (player->panim == PA_WALK)
					{
						if (speed > 12<<FRACBITS)
							mobj->tics = 2;
						else if (speed > 6<<FRACBITS)
							mobj->tics = 3;
						else
							mobj->tics = 4;
					}
379
					else if ((player->panim == PA_RUN) || (player->panim == PA_DASH))
380 381 382 383 384 385 386
					{
						if (speed > 52<<FRACBITS)
							mobj->tics = 1;
						else
							mobj->tics = 2;
					}
				}
Alam Ed Arias committed
387 388 389
			}
		}

390 391 392
		// Player animations
		if (st->sprite == SPR_PLAY)
		{
393
			skin_t *skin = ((skin_t *)mobj->skin);
394
			UINT16 frame = (mobj->frame & FF_FRAMEMASK)+1;
toaster committed
395
			UINT8 numframes, spr2;
396

397
			if (skin)
toaster committed
398
			{
lachwright committed
399
				spr2 = P_GetSkinSprite2(skin, (((player->powers[pw_super] && !(player->charflags & SF_NOSUPERSPRITES)) ? FF_SPR2SUPER : 0)|st->frame) & FF_FRAMEMASK, mobj->player);
400
				numframes = skin->sprites[spr2].numframes;
toaster committed
401
			}
402
			else
403
			{
toaster committed
404
				spr2 = 0;
405 406 407 408
				frame = 0;
				numframes = 0;
			}

409 410 411 412 413 414
			if (mobj->sprite != SPR_PLAY)
			{
				mobj->sprite = SPR_PLAY;
				frame = 0;
			}
			else if (mobj->sprite2 != spr2)
415
			{
416
				if ((st->frame & FF_SPR2MIDSTART) && numframes && P_RandomChance(FRACUNIT/2))
417 418 419 420 421 422 423
					frame = numframes/2;
				else
					frame = 0;
			}

			if (frame >= numframes)
			{
toaster committed
424
				if (st->frame & FF_SPR2ENDSTATE) // no frame advancement
425
				{
426
					if (st->var1 == mobj->state-states)
toaster committed
427
						frame--;
428
					else
toaster committed
429 430 431
					{
						if (mobj->frame & FF_FRAMEMASK)
							mobj->frame--;
432
						return P_SetPlayerMobjState(mobj, st->var1);
toaster committed
433
					}
434 435 436 437
				}
				else
					frame = 0;
			}
Yukita Mayako committed
438

439 440
			mobj->sprite2 = spr2;
			mobj->frame = frame|(st->frame&~FF_FRAMEMASK);
441
			if (P_PlayerFullbright(player))
442
				mobj->frame |= FF_FULLBRIGHT;
443 444 445 446 447 448 449
		}
		// Regular sprites
		else
		{
			mobj->sprite = st->sprite;
			mobj->frame = st->frame;
		}
Alam Ed Arias committed
450

451 452
		P_SetupStateAnimation(mobj, st);

Alam Ed Arias committed
453 454 455 456 457 458 459 460 461
		// Modified handling.
		// Call action functions when the state is set

		if (st->action.acp1)
		{
			var1 = st->var1;
			var2 = st->var2;
			astate = st;
			st->action.acp1(mobj);
Alam Ed Arias committed
462 463 464 465 466

			// 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 committed
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
		}

		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;
515 516 517 518

		// Player animations
		if (st->sprite == SPR_PLAY)
		{
519
			skin_t *skin = ((skin_t *)mobj->skin);
520
			UINT16 frame = (mobj->frame & FF_FRAMEMASK)+1;
toaster committed
521
			UINT8 numframes, spr2;
522

523
			if (skin)
toaster committed
524 525
			{
				spr2 = P_GetSkinSprite2(skin, st->frame & FF_FRAMEMASK, mobj->player);
526
				numframes = skin->sprites[spr2].numframes;
toaster committed
527
			}
528 529
			else
			{
toaster committed
530
				spr2 = 0;
531 532 533 534
				frame = 0;
				numframes = 0;
			}

535 536 537 538 539 540
			if (mobj->sprite != SPR_PLAY)
			{
				mobj->sprite = SPR_PLAY;
				frame = 0;
			}
			else if (mobj->sprite2 != spr2)
541
			{
542
				if ((st->frame & FF_SPR2MIDSTART) && numframes && P_RandomChance(FRACUNIT/2))
543 544 545 546 547 548 549
					frame = numframes/2;
				else
					frame = 0;
			}

			if (frame >= numframes)
			{
550 551 552 553 554 555 556 557 558 559 560
				if (st->frame & FF_SPR2ENDSTATE) // no frame advancement
				{
					if (st->var1 == mobj->state-states)
						frame--;
					else
					{
						if (mobj->frame & FF_FRAMEMASK)
							mobj->frame--;
						return P_SetMobjState(mobj, st->var1);
					}
				}
561 562 563 564
				else
					frame = 0;
			}

565 566 567 568 569 570 571 572 573
			mobj->sprite2 = spr2;
			mobj->frame = frame|(st->frame&~FF_FRAMEMASK);
		}
		// Regular sprites
		else
		{
			mobj->sprite = st->sprite;
			mobj->frame = st->frame;
		}
Alam Ed Arias committed
574

575 576
		P_SetupStateAnimation(mobj, st);

Alam Ed Arias committed
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
		// Modified handling.
		// Call action functions when the state is set

		if (st->action.acp1)
		{
			var1 = st->var1;
			var2 = st->var2;
			astate = st;
			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;
627
	P_SetupStateAnimation(mobj, st);
628

Alam Ed Arias committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	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;
646
	P_SetupStateAnimation((mobj_t*)mobj, st);
647

Alam Ed Arias committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	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;
	}

714
	for (think = thlist[THINK_MOBJ].next; think != &thlist[THINK_MOBJ]; think = think->next)
Alam Ed Arias committed
715
	{
716 717 718
		if (think->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed)
			continue;

Alam Ed Arias committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
		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;

854
			spawnpoints[j]->threshold = emeraldspawndelay + P_RandomByte() * (TICRATE/5);
Alam Ed Arias committed
855 856 857
			break;
		}
	}
858 859

	emeraldspawndelay = 0;
Alam Ed Arias committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
}

//
// 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)
	{
879
		P_RadiusAttack(mo, mo, 96*FRACUNIT, 0, true);
Alam Ed Arias committed
880 881 882 883

		explodemo = P_SpawnMobj(mo->x, mo->y, mo->z, MT_EXPLODE);
		P_SetScale(explodemo, mo->scale);
		explodemo->destscale = mo->destscale;
Inuyasha committed
884 885
		explodemo->momx += (P_RandomByte() % 32) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy += (P_RandomByte() % 32) * FixedMul(FRACUNIT/8, explodemo->scale);
Alam Ed Arias committed
886 887 888 889
		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;
Inuyasha committed
890 891
		explodemo->momx += (P_RandomByte() % 64) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy -= (P_RandomByte() % 64) * FixedMul(FRACUNIT/8, explodemo->scale);
Alam Ed Arias committed
892 893 894 895
		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;
Inuyasha committed
896 897
		explodemo->momx -= (P_RandomByte() % 128) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy += (P_RandomByte() % 128) * FixedMul(FRACUNIT/8, explodemo->scale);
Alam Ed Arias committed
898 899 900 901
		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;
Inuyasha committed
902 903
		explodemo->momx -= (P_RandomByte() % 96) * FixedMul(FRACUNIT/8, explodemo->scale);
		explodemo->momy -= (P_RandomByte() % 96) * FixedMul(FRACUNIT/8, explodemo->scale);
Alam Ed Arias committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
		S_StartSound(explodemo, sfx_cybdth);
	}

	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)
{
923
	fixed_t topheight, bottomheight;
Alam Ed Arias committed
924 925 926 927 928 929 930
	if (!(rover->flags & FF_EXISTS))
		return false;

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

931 932
	topheight    = P_GetFFloorTopZAt   (rover, mobj->x, mobj->y);
	bottomheight = P_GetFFloorBottomZAt(rover, mobj->x, mobj->y);
933 934

	if (mobj->z > topheight)
Alam Ed Arias committed
935 936
		return false;

937
	if (mobj->z + mobj->height < bottomheight)
Alam Ed Arias committed
938 939 940 941 942
		return false;

	return true;
}

943 944 945
// P_GetFloorZ (and its ceiling counterpart)
// Gets the floor height (or ceiling height) of the mobj's contact point in sector, assuming object's center if moved to [x, y]
// If line is supplied, it's a divider line on the sector. Set it to NULL if you're not checking for collision with a line
946 947 948 949
// Supply boundsec ONLY when checking for specials! It should be the "in-level" sector, and sector the control sector (if separate).
// If set, then this function will iterate through boundsec's linedefs to find the highest contact point on the slope. Non-special-checking
// usage will handle that later.
static fixed_t HighestOnLine(fixed_t radius, fixed_t x, fixed_t y, line_t *line, pslope_t *slope, boolean actuallylowest)
950
{
951 952 953 954 955 956 957 958 959 960 961 962
	// Alright, so we're sitting on a line that contains our slope sector, and need to figure out the highest point we're touching...
	// The solution is simple! Get the line's vertices, and pull each one in along its line until it touches the object's bounding box
	// (assuming it isn't already inside), then test each point's slope Z and return the higher of the two.
	vertex_t v1, v2;
	v1.x = line->v1->x;
	v1.y = line->v1->y;
	v2.x = line->v2->x;
	v2.y = line->v2->y;

	/*CONS_Printf("BEFORE: v1 = %f %f %f\n",
				FIXED_TO_FLOAT(v1.x),
				FIXED_TO_FLOAT(v1.y),
963
				FIXED_TO_FLOAT(P_GetSlopeZAt(slope, v1.x, v1.y))
964 965 966 967
				);
	CONS_Printf("        v2 = %f %f %f\n",
				FIXED_TO_FLOAT(v2.x),
				FIXED_TO_FLOAT(v2.y),
968
				FIXED_TO_FLOAT(P_GetSlopeZAt(slope, v2.x, v2.y))
969 970 971 972 973 974 975 976 977 978 979 980
				);*/

	if (abs(v1.x-x) > radius) {
		// v1's x is out of range, so rein it in
		fixed_t diff = abs(v1.x-x) - radius;

		if (v1.x < x) { // Moving right
			v1.x += diff;
			v1.y += FixedMul(diff, FixedDiv(line->dy, line->dx));
		} else { // Moving left
			v1.x -= diff;
			v1.y -= FixedMul(diff, FixedDiv(line->dy, line->dx));
981
		}
982
	}
983

984 985 986
	if (abs(v1.y-y) > radius) {
		// v1's y is out of range, so rein it in
		fixed_t diff = abs(v1.y-y) - radius;
987

988 989 990 991 992 993 994 995
		if (v1.y < y) { // Moving up
			v1.y += diff;
			v1.x += FixedMul(diff, FixedDiv(line->dx, line->dy));
		} else { // Moving down
			v1.y -= diff;
			v1.x -= FixedMul(diff, FixedDiv(line->dx, line->dy));
		}
	}
996

997 998 999
	if (abs(v2.x-x) > radius) {
		// v1's x is out of range, so rein it in
		fixed_t diff = abs(v2.x-x) - radius;
1000

1001 1002 1003 1004 1005 1006 1007 1008
		if (v2.x < x) { // Moving right
			v2.x += diff;
			v2.y += FixedMul(diff, FixedDiv(line->dy, line->dx));
		} else { // Moving left
			v2.x -= diff;
			v2.y -= FixedMul(diff, FixedDiv(line->dy, line->dx));
		}
	}
1009

1010 1011 1012
	if (abs(v2.y-y) > radius) {
		// v2's y is out of range, so rein it in
		fixed_t diff = abs(v2.y-y) - radius;
1013

1014 1015 1016 1017 1018 1019
		if (v2.y < y) { // Moving up
			v2.y += diff;
			v2.x += FixedMul(diff, FixedDiv(line->dx, line->dy));
		} else { // Moving down
			v2.y -= diff;
			v2.x -= FixedMul(diff, FixedDiv(line->dx, line->dy));
1020
		}
1021 1022 1023 1024 1025
	}

	/*CONS_Printf("AFTER:  v1 = %f %f %f\n",
				FIXED_TO_FLOAT(v1.x),
				FIXED_TO_FLOAT(v1.y),
1026
				FIXED_TO_FLOAT(P_GetSlopeZAt(slope, v1.x, v1.y))
1027 1028 1029 1030
				);
	CONS_Printf("        v2 = %f %f %f\n",
				FIXED_TO_FLOAT(v2.x),
				FIXED_TO_FLOAT(v2.y),
1031
				FIXED_TO_FLOAT(P_GetSlopeZAt(slope, v2.x, v2.y))
1032 1033 1034 1035 1036
				);*/

	// Return the higher of the two points
	if (actuallylowest)
		return min(
1037 1038
			P_GetSlopeZAt(slope, v1.x, v1.y),
			P_GetSlopeZAt(slope, v2.x, v2.y)
1039 1040 1041
		);
	else
		return max(
1042 1043
			P_GetSlopeZAt(slope, v1.x, v1.y),
			P_GetSlopeZAt(slope, v2.x, v2.y)
1044
		);
1045 1046
}

1047
fixed_t P_MobjFloorZ(mobj_t *mobj, sector_t *sector, sector_t *boundsec, fixed_t x, fixed_t y, line_t *line, boolean lowest, boolean perfect)
1048 1049
{
	I_Assert(mobj != NULL);
1050
	I_Assert(sector != NULL);
LJ Sonic committed
1051

1052
	if (sector->f_slope) {
1053
		fixed_t testx, testy;
1054
		pslope_t *slope = sector->f_slope;
1055

1056
		// Get the corner of the object that should be the highest on the slope
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		if (slope->d.x < 0)
			testx = mobj->radius;
		else
			testx = -mobj->radius;

		if (slope->d.y < 0)
			testy = mobj->radius;
		else
			testy = -mobj->radius;

1067
		if ((slope->zdelta > 0) ^ !!(lowest)) {
1068
			testx = -testx;
RedEnchilada committed
1069
			testy = -testy;
1070
		}
1071

1072 1073
		testx += x;
		testy += y;
1074

1075
		// If the highest point is in the sector, then we have it easy! Just get the Z at that point
1076
		if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector))
1077
			return P_GetSlopeZAt(slope, testx, testy);
1078

1079 1080 1081 1082 1083 1084
		// If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point
		if (perfect) {
			size_t i;
			line_t *ld;
			fixed_t bbox[4];
			fixed_t finalheight;
1085

1086 1087 1088 1089
			if (lowest)
				finalheight = INT32_MAX;
			else
				finalheight = INT32_MIN;
1090

1091 1092 1093 1094 1095 1096
			bbox[BOXLEFT] = x-mobj->radius;
			bbox[BOXRIGHT] = x+mobj->radius;
			bbox[BOXTOP] = y+mobj->radius;
			bbox[BOXBOTTOM] = y-mobj->radius;
			for (i = 0; i < boundsec->linecount; i++) {
				ld = boundsec->lines[i];
1097

1098 1099 1100
				if (bbox[BOXRIGHT] <= ld->bbox[BOXLEFT] || bbox[BOXLEFT] >= ld->bbox[BOXRIGHT]
				|| bbox[BOXTOP] <= ld->bbox[BOXBOTTOM] || bbox[BOXBOTTOM] >= ld->bbox[BOXTOP])
					continue;
1101

1102 1103
				if (P_BoxOnLineSide(bbox, ld) != -1)
					continue;
1104

1105 1106 1107 1108
				if (lowest)
					finalheight = min(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, true));
				else
					finalheight = max(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, false));
1109 1110
			}

1111 1112
			return finalheight;
		}
1113

1114 1115 1116
		// If we're just testing for base sector location (no collision line), just go for the center's spot...
		// It'll get fixed when we test for collision anyway, and the final result can't be lower than this
		if (line == NULL)
1117
			return P_GetSlopeZAt(slope, x, y);
1118

1119 1120 1121
		return HighestOnLine(mobj->radius, x, y, line, slope, lowest);
	} else // Well, that makes it easy. Just get the floor height
		return sector->floorheight;
1122 1123
}

1124
fixed_t P_MobjCeilingZ(mobj_t *mobj, sector_t *sector, sector_t *boundsec, fixed_t x, fixed_t y, line_t *line, boolean lowest, boolean perfect)
1125 1126 1127
{
	I_Assert(mobj != NULL);
	I_Assert(sector != NULL);
LJ Sonic committed
1128

1129
	if (sector->c_slope) {
1130
		fixed_t testx, testy;
1131
		pslope_t *slope = sector->c_slope;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

		// Get the corner of the object that should be the highest on the slope
		if (slope->d.x < 0)
			testx = mobj->radius;
		else
			testx = -mobj->radius;

		if (slope->d.y < 0)
			testy = mobj->radius;
		else
			testy = -mobj->radius;

1144
		if ((slope->zdelta > 0) ^ !!(lowest)) {
1145 1146 1147 1148 1149 1150 1151 1152
			testx = -testx;
			testy = -testy;
		}

		testx += x;
		testy += y;

		// If the highest point is in the sector, then we have it easy! Just get the Z at that point
1153
		if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector))
1154
			return P_GetSlopeZAt(slope, testx, testy);
1155

1156 1157 1158 1159 1160 1161
		// If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point
		if (perfect) {
			size_t i;
			line_t *ld;
			fixed_t bbox[4];
			fixed_t finalheight;
1162

1163 1164 1165 1166
			if (lowest)
				finalheight = INT32_MAX;
			else
				finalheight = INT32_MIN;
1167

1168 1169 1170 1171 1172 1173
			bbox[BOXLEFT] = x-mobj->radius;
			bbox[BOXRIGHT] = x+mobj->radius;
			bbox[BOXTOP] = y+mobj->radius;
			bbox[BOXBOTTOM] = y-mobj->radius;
			for (i = 0; i < boundsec->linecount; i++) {
				ld = boundsec->lines[i];
1174

1175 1176 1177
				if (bbox[BOXRIGHT] <= ld->bbox[BOXLEFT] || bbox[BOXLEFT] >= ld->bbox[BOXRIGHT]
				|| bbox[BOXTOP] <= ld->bbox[BOXBOTTOM] || bbox[BOXBOTTOM] >= ld->bbox[BOXTOP])
					continue;
1178

1179 1180
				if (P_BoxOnLineSide(bbox, ld) != -1)
					continue;
1181

1182 1183 1184 1185
				if (lowest)
					finalheight = min(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, true));
				else
					finalheight = max(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, false));
1186 1187
			}

1188
			return finalheight;
1189 1190 1191
		}

		// If we're just testing for base sector location (no collision line), just go for the center's spot...
1192
		// It'll get fixed when we test for collision anyway, and the final result can't be lower than this
1193
		if (line == NULL)
1194
			return P_GetSlopeZAt(slope, x, y);
1195

1196 1197 1198
		return HighestOnLine(mobj->radius, x, y, line, slope, lowest);
	} else // Well, that makes it easy. Just get the ceiling height
		return sector->ceilingheight;
1199 1200
}

1201
// Now do the same as all above, but for cameras because apparently cameras are special?
1202
fixed_t P_CameraFloorZ(camera_t *mobj, sector_t *sector, sector_t *boundsec, fixed_t x, fixed_t y, line_t *line, boolean lowest, boolean perfect)
1203 1204 1205
{
	I_Assert(mobj != NULL);
	I_Assert(sector != NULL);
LJ Sonic committed
1206

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	if (sector->f_slope) {
		fixed_t testx, testy;
		pslope_t *slope = sector->f_slope;

		// Get the corner of the object that should be the highest on the slope
		if (slope->d.x < 0)
			testx = mobj->radius;
		else
			testx = -mobj->radius;

		if (slope->d.y < 0)
			testy = mobj->radius;
		else
			testy = -mobj->radius;

1222
		if ((slope->zdelta > 0) ^ !!(lowest)) {
1223 1224 1225 1226 1227 1228 1229 1230
			testx = -testx;
			testy = -testy;
		}

		testx += x;
		testy += y;

		// If the highest point is in the sector, then we have it easy! Just get the Z at that point
1231
		if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector))
1232
			return P_GetSlopeZAt(slope, testx, testy);
1233

1234 1235 1236 1237 1238 1239
		// If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point
		if (perfect) {
			size_t i;
			line_t *ld;
			fixed_t bbox[4];
			fixed_t finalheight;
1240

1241 1242 1243 1244
			if (lowest)
				finalheight = INT32_MAX;
			else
				finalheight = INT32_MIN;
1245

1246 1247 1248 1249 1250 1251
			bbox[BOXLEFT] = x-mobj->radius;
			bbox[BOXRIGHT] = x+mobj->radius;
			bbox[BOXTOP] = y+mobj->radius;
			bbox[BOXBOTTOM] = y-mobj->radius;
			for (i = 0; i < boundsec->linecount; i++) {
				ld = boundsec->lines[i];
1252

1253 1254 1255
				if (bbox[BOXRIGHT] <= ld->bbox[BOXLEFT] || bbox[BOXLEFT] >= ld->bbox[BOXRIGHT]
				|| bbox[BOXTOP] <= ld->bbox[BOXBOTTOM] || bbox[BOXBOTTOM] >= ld->bbox[BOXTOP])
					continue;
1256

1257 1258
				if (P_BoxOnLineSide(bbox, ld) != -1)
					continue;
1259

1260 1261 1262 1263
				if (lowest)
					finalheight = min(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, true));
				else
					finalheight = max(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, false));
1264 1265
			}

1266
			return finalheight;
1267 1268 1269
		}

		// If we're just testing for base sector location (no collision line), just go for the center's spot...
1270
		// It'll get fixed when we test for collision anyway, and the final result can't be lower than this
1271
		if (line == NULL)
1272
			return P_GetSlopeZAt(slope, x, y);
1273

1274 1275 1276
		return HighestOnLine(mobj->radius, x, y, line, slope, lowest);
	} else // Well, that makes it easy. Just get the floor height
		return sector->floorheight;
1277 1278
}

1279
fixed_t P_CameraCeilingZ(camera_t *mobj, sector_t *sector, sector_t *boundsec, fixed_t x, fixed_t y, line_t *line, boolean lowest, boolean perfect)
1280 1281 1282
{
	I_Assert(mobj != NULL);
	I_Assert(sector != NULL);
LJ Sonic committed
1283

1284
	if (sector->c_slope) {
1285
		fixed_t testx, testy;
1286
		pslope_t *slope = sector->c_slope;
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298

		// Get the corner of the object that should be the highest on the slope
		if (slope->d.x < 0)
			testx = mobj->radius;
		else
			testx = -mobj->radius;

		if (slope->d.y < 0)
			testy = mobj->radius;
		else
			testy = -mobj->radius;

1299
		if ((slope->zdelta > 0) ^ !!(lowest)) {
1300 1301 1302 1303 1304 1305 1306 1307
			testx = -testx;
			testy = -testy;
		}

		testx += x;
		testy += y;

		// If the highest point is in the sector, then we have it easy! Just get the Z at that point
1308
		if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector))
1309
			return P_GetSlopeZAt(slope, testx, testy);
1310

1311 1312 1313 1314 1315 1316
		// If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point
		if (perfect) {
			size_t i;
			line_t *ld;
			fixed_t bbox[4];
			fixed_t finalheight;
1317

1318 1319 1320 1321
			if (lowest)
				finalheight = INT32_MAX;
			else
				finalheight = INT32_MIN;
1322

1323 1324 1325 1326 1327 1328
			bbox[BOXLEFT] = x-mobj->radius;
			bbox[BOXRIGHT] = x+mobj->radius;
			bbox[BOXTOP] = y+mobj->radius;
			bbox[BOXBOTTOM] = y-mobj->radius;
			for (i = 0; i < boundsec->linecount; i++) {
				ld = boundsec->lines[i];
1329

1330 1331 1332
				if (bbox[BOXRIGHT] <= ld->bbox[BOXLEFT] || bbox[BOXLEFT] >= ld->bbox[BOXRIGHT]
				|| bbox[BOXTOP] <= ld->bbox[BOXBOTTOM] || bbox[BOXBOTTOM] >= ld->bbox[BOXTOP])
					continue;
1333

1334 1335
				if (P_BoxOnLineSide(bbox, ld) != -1)
					continue;
1336

1337 1338 1339 1340
				if (lowest)
					finalheight = min(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, true));
				else
					finalheight = max(finalheight, HighestOnLine(mobj->radius, x, y, ld, slope, false));
1341 1342
			}

1343
			return finalheight;
1344 1345 1346
		}

		// If we're just testing for base sector location (no collision line), just go for the center's spot...
1347
		// It'll get fixed when we test for collision anyway, and the final result can't be lower than this
1348
		if (line == NULL)
1349
			return P_GetSlopeZAt(slope, x, y);
1350

1351 1352 1353
		return HighestOnLine(mobj->radius, x, y, line, slope, lowest);
	} else // Well, that makes it easy. Just get the ceiling height
		return sector->ceilingheight;
1354
}
Alam Ed Arias committed
1355 1356 1357 1358 1359 1360 1361 1362
static void P_PlayerFlip(mobj_t *mo)
{
	if (!mo->player)
		return;

	G_GhostAddFlip();
	// Flip aiming to match!

1363 1364
	if (mo->player->powers[pw_carry] == CR_NIGHTSMODE) // NiGHTS doesn't use flipcam
		;
Alam Ed Arias committed
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
	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 committed
1391
//
1392
// P_GetMobjGravity
Alam Ed Arias committed
1393
//
1394 1395
// Returns the current gravity
// value of the object.
Alam Ed Arias committed
1396
//
1397
fixed_t P_GetMobjGravity(mobj_t *mo)
Alam Ed Arias committed
1398 1399 1400 1401
{
	fixed_t gravityadd = 0;
	boolean no3dfloorgrav = true; // Custom gravity
	boolean goopgravity = false;
Alam Ed Arias committed
1402
	boolean wasflip;
Alam Ed Arias committed
1403 1404 1405 1406

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

Alam Ed Arias committed
1407 1408
	wasflip = (mo->eflags & MFE_VERTICALFLIP) != 0;

Alam Ed Arias committed
1409 1410 1411 1412 1413 1414 1415 1416 1417
	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)
		{
toaster committed
1418
			if (!(rover->flags & FF_EXISTS) || !P_InsideANonSolidFFloor(mo, rover)) // P_InsideANonSolidFFloor checks for FF_EXISTS itself, but let's not always call this function
Alam Ed Arias committed
1419 1420
				continue;

1421 1422
			if ((rover->flags & (FF_SWIMMABLE|FF_GOOWATER)) == (FF_SWIMMABLE|FF_GOOWATER))
				goopgravity = true;
Alam Ed Arias committed
1423

1424 1425
			if (!(rover->master->frontsector->gravity))
				continue;
Alam Ed Arias committed
1426

1427 1428 1429 1430 1431 1432 1433 1434
			gravityadd = -FixedMul(gravity,
				(FixedDiv(*rover->master->frontsector->gravity>>FRACBITS, 1000)));

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

			no3dfloorgrav = false;
			break;
Alam Ed Arias committed
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
		}
	}

	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->player)
	{
1456
		if ((mo->player->pflags & PF_GLIDING)
1457
		|| (mo->player->charability == CA_FLY && mo->player->panim == PA_ABILITY))
1458
			gravityadd = gravityadd/3; // less gravity while flying/gliding
1459
		if (mo->player->climbing || (mo->player->powers[pw_carry] == CR_NIGHTSMODE))
Alam Ed Arias committed
1460 1461
			gravityadd = 0;

1462
		if (!(mo->flags2 & MF2_OBJECTFLIP) != !(mo->player->powers[pw_gravityboots])) // negated to turn numeric into bool - would be double negated, but not needed if both would be
Alam Ed Arias committed
1463
		{
toaster committed
1464 1465
			gravityadd = -gravityadd;
			mo->eflags ^= MFE_VERTICALFLIP;
Alam Ed Arias committed
1466
		}
wolfs committed
1467
		if (wasflip == !(mo->eflags & MFE_VERTICALFLIP)) // note!! == ! is not equivalent to != here - turns numeric into bool this way
1468
			P_PlayerFlip(mo);
Alam Ed Arias committed
1469 1470 1471 1472 1473 1474 1475 1476 1477
	}
	else
	{
		// Objects with permanent reverse gravity (MF2_OBJECTFLIP)
		if (mo->flags2 & MF2_OBJECTFLIP)
		{
			mo->eflags |= MFE_VERTICALFLIP;
			if (mo->z + mo->height >= mo->ceilingz)
				gravityadd = 0;
1478 1479
			else if (gravityadd < 0) // Don't sink, only rise up
				gravityadd *= -1;
Alam Ed Arias committed
1480 1481 1482 1483 1484 1485 1486
		}
		else //Otherwise, sort through the other exceptions.
		{
			switch (mo->type)
			{
				case MT_FLINGRING:
				case MT_FLINGCOIN:
1487 1488
				case MT_FLINGBLUESPHERE:
				case MT_FLINGNIGHTSCHIP:
Alam Ed Arias committed
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
				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:
toaster committed
1516
				case MT_CYBRAKDEMON:
Alam Ed Arias committed
1517 1518 1519 1520 1521 1522 1523 1524 1525
					gravityadd >>= 1;
				default:
					break;
			}
		}
	}

	// Goop has slower, reversed gravity
	if (goopgravity)
1526
		gravityadd = -((gravityadd/5) + (gravityadd/8));
Alam Ed Arias committed
1527

1528
	gravityadd = FixedMul(gravityadd, mo->scale);
Alam Ed Arias committed
1529

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
	return gravityadd;
}

//
// 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 = P_GetMobjGravity(mo);

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

1547
	if (affect)
1548
		mo->momz += gravityadd;
Alam Ed Arias committed
1549

Alam Ed Arias committed
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576