//-------------------------------------------------------------------------
/*
Copyright (C) 2021 Christoph Oelckers & Mitchell Richters

This is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/
//------------------------------------------------------------------------- 

#include "gamefuncs.h"
#include "gamestruct.h"


//---------------------------------------------------------------------------
//
// Unified chasecam function for all games.
//
//---------------------------------------------------------------------------

int cameradist, cameraclock;

bool calcChaseCamPos(int* px, int* py, int* pz, spritetype* pspr, short *psectnum, binangle ang, fixedhoriz horiz, double const smoothratio)
{
	hitdata_t hitinfo;
	binangle daang;
	short bakcstat;
	int newdist;

	assert(*psectnum >= 0 && *psectnum < MAXSECTORS);

	// Calculate new pos to shoot backwards, using averaged values from the big three.
	int nx = gi->chaseCamX(ang);
	int ny = gi->chaseCamY(ang);
	int nz = gi->chaseCamZ(horiz);

	vec3_t pvect = { *px, *py, *pz };
	bakcstat = pspr->cstat;
	pspr->cstat &= ~(CSTAT_SPRITE_BLOCK | CSTAT_SPRITE_BLOCK_HITSCAN);
	updatesectorz(*px, *py, *pz, psectnum);
	hitscan(&pvect, *psectnum, nx, ny, nz, &hitinfo, CLIPMASK1);
	pspr->cstat = bakcstat;

	int hx = hitinfo.pos.x - *px;
	int hy = hitinfo.pos.y - *py;

	if (*psectnum < 0)
	{
		return false;
	}

	assert(*psectnum >= 0 && *psectnum < MAXSECTORS);

	// If something is in the way, make pp->camera_dist lower if necessary
	if (abs(nx) + abs(ny) > abs(hx) + abs(hy))
	{
		if (hitinfo.wall >= 0)
		{
			// Push you a little bit off the wall
			*psectnum = hitinfo.sect;
			daang = bvectangbam(wall[wall[hitinfo.wall].point2].x - wall[hitinfo.wall].x,
								wall[wall[hitinfo.wall].point2].y - wall[hitinfo.wall].y);
			newdist = nx * daang.bsin() + ny * -daang.bcos();

			if (abs(nx) > abs(ny))
				hx -= MulScale(nx, newdist, 28);
			else
				hy -= MulScale(ny, newdist, 28);
		}
		else if (hitinfo.sprite < 0)		
		{
			// Push you off the ceiling/floor
			*psectnum = hitinfo.sect;

			if (abs(nx) > abs(ny))
				hx -= (nx >> 5);
			else
				hy -= (ny >> 5);
		}
		else
		{
			// If you hit a sprite that's not a wall sprite - try again.
			spritetype* hspr = &sprite[hitinfo.sprite];

			if (!(hspr->cstat & CSTAT_SPRITE_ALIGNMENT_WALL))
			{
				bakcstat = hspr->cstat;
				hspr->cstat &= ~(CSTAT_SPRITE_BLOCK | CSTAT_SPRITE_BLOCK_HITSCAN);
				calcChaseCamPos(px, py, pz, pspr, psectnum, ang, horiz, smoothratio);
				hspr->cstat = bakcstat;
				return false;
			}
			else
			{
				// same as wall calculation.
				daang = buildang(pspr->ang - 512);
				newdist = nx * daang.bsin() + ny * -daang.bcos();

				if (abs(nx) > abs(ny))
					hx -= MulScale(nx, newdist, 28);
				else
					hy -= MulScale(ny, newdist, 28);
			}
		}

		if (abs(nx) > abs(ny))
			newdist = DivScale(hx, nx, 16);
		else
			newdist = DivScale(hy, ny, 16);

		if (newdist < cameradist)
			cameradist = newdist;
	}

	// Actually move you! (Camerdist is 65536 if nothing is in the way)
	*px += MulScale(nx, cameradist, 16);
	*py += MulScale(ny, cameradist, 16);
	*pz += MulScale(nz, cameradist, 16);

	// Caculate clock using GameTicRate so it increases the same rate on all speed computers.
	int myclock = PlayClock + MulScale(120 / GameTicRate, smoothratio, 16);
	if (cameraclock == INT_MIN)
	{
		// Third person view was just started.
		cameraclock = myclock;
	}

	// Slowly increase cameradist until it reaches 65536.
	cameradist = min(cameradist + ((myclock - cameraclock) << 10), 65536);
	cameraclock = myclock;

	// Make sure psectnum is correct.
	updatesectorz(*px, *py, *pz, psectnum);

	return true;
}