/*
	PostNuke Camscript (0.3e)
	Author: NeoPhoenix
	Date: 31.03.2012

	
	native MoveCamera(playerid, Float:dest_x, Float:dest_y, Float:dest_z, Float:lookat_x, Float:lookat_y, Float:lookat_z, Float:speed,source=0);
	native SetPlayerCameraInfoEx(playerid, Float:cam_pos[3], Float:cam_look[3]);
	native Float:GetDistanceBetweenPoints(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2);
	forward OnPlayerFinishCameraMovement(playerid);
*/

forward OnPlayerFinishCameraMovement(playerid);
forward Float:GetDistanceBetweenPoints(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2);

Float:GetDistanceBetweenPoints(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{
	return floatsqroot(floatpower(x1 - x2, 2) + floatpower(y1 - y2, 2) + floatpower(z1 - z2, 2));
}

enum CAMMOVEMENT 
{
	camTIME_MS,
	Float:camPOSfrom[3],
	Float:camLOOKfrom[3],
	Float:camPOSto[3],
	Float:camLOOKto[3],
	camTIMER,
	bool:camMoving
}
new PlayerCamera[MAX_PLAYERS][CAMMOVEMENT];

#define SLOW 0.3
#define FAST 3.0

MovePlayerCamera(playerid, Float:cam_pos_to[3], Float:cam_look_to[3], Float:speed=SLOW, source=0)
{
	new Float:distance;
	if(PlayerCamera[playerid][camMoving] == true) KillTimer(PlayerCamera[playerid][camTIMER]);
	PlayerCamera[playerid][camMoving] = true;
	if(source != 1)	
	{
		distance = GetDistanceBetweenPoints(\
			PlayerCamera[playerid][camPOSfrom][0],\
			PlayerCamera[playerid][camPOSfrom][1],\
			PlayerCamera[playerid][camPOSfrom][2],\
			cam_pos_to[0],\
			cam_pos_to[1],\
			cam_pos_to[2]);//CamPos
	}
	else 
	{
		distance = GetDistanceBetweenPoints(\
			PlayerCamera[playerid][camLOOKfrom][0],\
			PlayerCamera[playerid][camLOOKfrom][1],\
			PlayerCamera[playerid][camLOOKfrom][2],\
			cam_look_to[0],\
			cam_look_to[1],\
			cam_look_to[2]);//CamLook
	}
	new time_ms = floatround((distance/speed)*45);
	
	for(new i; i<3; i++)
	{
		PlayerCamera[playerid][camPOSto][i]		= cam_pos_to[i];
		PlayerCamera[playerid][camLOOKto][i]	= cam_look_to[i];
	}
	
	if(time_ms > 0)
	{
		InterpolateCameraPos(playerid,
			PlayerCamera[playerid][camPOSfrom][0],
			PlayerCamera[playerid][camPOSfrom][1],
			PlayerCamera[playerid][camPOSfrom][2],
			cam_pos_to[0],
			cam_pos_to[1],
			cam_pos_to[2],
			time_ms,
			CAMERA_MOVE);	
		
		InterpolateCameraLookAt(playerid,
			PlayerCamera[playerid][camLOOKfrom][0],
			PlayerCamera[playerid][camLOOKfrom][1],
			PlayerCamera[playerid][camLOOKfrom][2],
			cam_look_to[0],
			cam_look_to[1],
			cam_look_to[2],
			time_ms,
			CAMERA_MOVE);

		PlayerCamera[playerid][camTIMER] = SetTimerEx("OnPlayerFinishCameraMovement", time_ms, false, "i", playerid);
	}
	else
	{
		SetPlayerCameraPos(playerid,cam_pos_to[0],cam_pos_to[1],cam_pos_to[2]);
		SetPlayerCameraLookAt(playerid,cam_look_to[0],cam_look_to[1],cam_look_to[2]);
		OnPlayerFinishCameraMovement(playerid);
	}
	return 1;
}

SetPlayerCameraInfoEx(playerid, Float:cam_pos[3], Float:cam_look[3])
{
    SetPlayerCameraPos(playerid, 	cam_pos[0], cam_pos[1], cam_pos[2] );
    SetPlayerCameraLookAt(playerid, cam_look[0],cam_look[1],cam_look[2]);
	for(new i; i<3; i++)
	{
		PlayerCamera[playerid][camPOSfrom][i]	= cam_pos[i];
		PlayerCamera[playerid][camLOOKfrom][i]	= cam_look[i];
		PlayerCamera[playerid][camPOSto][i]		= cam_pos[i];
		PlayerCamera[playerid][camLOOKto][i]	= cam_look[i];
	}
    return 1;
}