Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

655 lines
16 KiB

//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
9 years ago
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include "hud.h"
#include "cl_util.h"
#include "camera.h"
#include "kbutton.h"
#include "cvardef.h"
#include "usercmd.h"
#include "const.h"
#include "camera.h"
#include "in_defs.h"
9 years ago
float CL_KeyState( kbutton_t *key );
9 years ago
9 years ago
extern "C"
9 years ago
{
void DLLEXPORT CAM_Think( void );
int DLLEXPORT CL_IsThirdPerson( void );
void DLLEXPORT CL_CameraOffset( float *ofs );
}
extern cl_enginefunc_t gEngfuncs;
//-------------------------------------------------- Constants
#define CAM_DIST_DELTA 1.0f
#define CAM_ANGLE_DELTA 2.5f
#define CAM_ANGLE_SPEED 2.5f
#define CAM_MIN_DIST 30.0f
#define CAM_ANGLE_MOVE 0.5f
#define MAX_ANGLE_DIFF 10.0f
#define PITCH_MAX 90.0f
#define PITCH_MIN 0.0f
#define YAW_MAX 135.0f
#define YAW_MIN -135.0f
9 years ago
enum ECAM_Command
{
CAM_COMMAND_NONE = 0,
CAM_COMMAND_TOTHIRDPERSON = 1,
CAM_COMMAND_TOFIRSTPERSON = 2
};
//-------------------------------------------------- Global Variables
cvar_t *cam_command;
cvar_t *cam_snapto;
cvar_t *cam_idealyaw;
cvar_t *cam_idealpitch;
cvar_t *cam_idealdist;
cvar_t *cam_contain;
cvar_t *c_maxpitch;
cvar_t *c_minpitch;
cvar_t *c_maxyaw;
cvar_t *c_minyaw;
cvar_t *c_maxdistance;
cvar_t *c_mindistance;
// pitch, yaw, dist
vec3_t cam_ofs;
// In third person
int cam_thirdperson;
int cam_mousemove; //true if we are moving the cam with the mouse, False if not
9 years ago
int iMouseInUse = 0;
9 years ago
int cam_distancemove;
extern int mouse_x, mouse_y; //used to determine what the current x and y values are
int cam_old_mouse_x, cam_old_mouse_y; //holds the last ticks mouse movement
9 years ago
POINT cam_mouse;
9 years ago
//-------------------------------------------------- Local Variables
static kbutton_t cam_pitchup, cam_pitchdown, cam_yawleft, cam_yawright;
static kbutton_t cam_in, cam_out, cam_move;
//-------------------------------------------------- Prototypes
void CAM_ToThirdPerson(void);
void CAM_ToFirstPerson(void);
void CAM_StartDistance(void);
void CAM_EndDistance(void);
//-------------------------------------------------- Local Functions
float MoveToward( float cur, float goal, float maxspeed )
{
if( cur != goal )
{
if( fabs( cur - goal ) > 180.0f )
9 years ago
{
if( cur < goal )
cur += 360.0f;
9 years ago
else
cur -= 360.0f;
9 years ago
}
if( cur < goal )
{
if( cur < goal - 1.0f )
cur += ( goal - cur ) * 0.25f;
9 years ago
else
cur = goal;
}
else
{
if( cur > goal + 1.0f )
cur -= ( cur - goal ) * 0.25f;
9 years ago
else
cur = goal;
}
}
// bring cur back into range
if( cur < 0.0f )
cur += 360.0f;
else if( cur >= 360.0f )
cur -= 360.0f;
9 years ago
return cur;
}
//-------------------------------------------------- Gobal Functions
typedef struct
{
vec3_t boxmins, boxmaxs;// enclose the test object along entire move
float *mins, *maxs; // size of the moving object
vec3_t mins2, maxs2; // size when clipping against mosnters
float *start, *end;
trace_t trace;
9 years ago
int type;
9 years ago
edict_t *passedict;
qboolean monsterclip;
} moveclip_t;
9 years ago
extern trace_t SV_ClipMoveToEntity( edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end );
9 years ago
void DLLEXPORT CAM_Think( void )
{
vec3_t origin;
vec3_t ext, pnt, camForward, camRight, camUp;
moveclip_t clip;
float dist;
vec3_t camAngles;
float flSensitivity;
#ifdef LATER
int i;
#endif
vec3_t viewangles;
//++ BulliT
/* switch( (int)cam_command->value )
9 years ago
{
case CAM_COMMAND_TOTHIRDPERSON:
CAM_ToThirdPerson();
break;
case CAM_COMMAND_TOFIRSTPERSON:
CAM_ToFirstPerson();
break;
case CAM_COMMAND_NONE:
default:
break;
}
*/
//-- Martin Webrant
9 years ago
if( !cam_thirdperson )
return;
#ifdef LATER
9 years ago
if( cam_contain->value )
9 years ago
{
gEngfuncs.GetClientOrigin( origin );
ext[0] = ext[1] = ext[2] = 0.0f;
9 years ago
}
#endif
9 years ago
camAngles[PITCH] = cam_idealpitch->value;
camAngles[YAW] = cam_idealyaw->value;
9 years ago
dist = cam_idealdist->value;
9 years ago
9 years ago
//
//movement of the camera with the mouse
//
9 years ago
if( cam_mousemove )
9 years ago
{
9 years ago
//get windows cursor position
GetCursorPos( &cam_mouse );
9 years ago
//check for X delta values and adjust accordingly
//eventually adjust YAW based on amount of movement
9 years ago
//don't do any movement of the cam using YAW/PITCH if we are zooming in/out the camera
if( !cam_distancemove )
9 years ago
{
9 years ago
//keep the camera within certain limits around the player (ie avoid certain bad viewing angles)
if( cam_mouse.x>gEngfuncs.GetWindowCenterX() )
9 years ago
{
//if( ( camAngles[YAW] >= 225.0f ) || ( camAngles[YAW] < 135.0f ) )
9 years ago
if( camAngles[YAW] < c_maxyaw->value )
{
camAngles[YAW] += CAM_ANGLE_MOVE * ( ( cam_mouse.x - gEngfuncs.GetWindowCenterX() ) / 2 );
}
if( camAngles[YAW] > c_maxyaw->value )
{
camAngles[YAW] = c_maxyaw->value;
}
9 years ago
}
9 years ago
else if( cam_mouse.x<gEngfuncs.GetWindowCenterX() )
9 years ago
{
//if( ( camAngles[YAW] <= 135.0f ) || ( camAngles[YAW] > 225.0f ) )
9 years ago
if( camAngles[YAW] > c_minyaw->value )
{
camAngles[YAW] -= CAM_ANGLE_MOVE * ( ( gEngfuncs.GetWindowCenterX() - cam_mouse.x ) / 2 );
}
if( camAngles[YAW] < c_minyaw->value )
{
camAngles[YAW] = c_minyaw->value;
}
9 years ago
}
9 years ago
//check for y delta values and adjust accordingly
//eventually adjust PITCH based on amount of movement
//also make sure camera is within bounds
if( cam_mouse.y>gEngfuncs.GetWindowCenterY() )
9 years ago
{
9 years ago
if( camAngles[PITCH] < c_maxpitch->value )
{
camAngles[PITCH] += CAM_ANGLE_MOVE * ( ( cam_mouse.y - gEngfuncs.GetWindowCenterY() ) / 2 );
}
if( camAngles[PITCH] > c_maxpitch->value )
{
camAngles[PITCH] = c_maxpitch->value;
}
9 years ago
}
9 years ago
else if( cam_mouse.y<gEngfuncs.GetWindowCenterY() )
9 years ago
{
9 years ago
if( camAngles[PITCH] > c_minpitch->value )
{
camAngles[PITCH] -= CAM_ANGLE_MOVE * ( ( gEngfuncs.GetWindowCenterY() - cam_mouse.y ) / 2 );
}
if( camAngles[PITCH] < c_minpitch->value )
{
camAngles[PITCH] = c_minpitch->value;
}
9 years ago
}
9 years ago
//set old mouse coordinates to current mouse coordinates
//since we are done with the mouse
if( ( flSensitivity = gHUD.GetSensitivity() ) != 0 )
9 years ago
{
9 years ago
cam_old_mouse_x = cam_mouse.x * flSensitivity;
cam_old_mouse_y = cam_mouse.y * flSensitivity;
9 years ago
}
9 years ago
else
9 years ago
{
9 years ago
cam_old_mouse_x = cam_mouse.x;
cam_old_mouse_y = cam_mouse.y;
9 years ago
}
9 years ago
SetCursorPos( gEngfuncs.GetWindowCenterX(), gEngfuncs.GetWindowCenterY() );
9 years ago
}
}
//Nathan code here
if( CL_KeyState( &cam_pitchup ) )
9 years ago
camAngles[PITCH] += CAM_ANGLE_DELTA;
9 years ago
else if( CL_KeyState( &cam_pitchdown ) )
9 years ago
camAngles[PITCH] -= CAM_ANGLE_DELTA;
9 years ago
if( CL_KeyState( &cam_yawleft ) )
9 years ago
camAngles[YAW] -= CAM_ANGLE_DELTA;
9 years ago
else if( CL_KeyState( &cam_yawright ) )
9 years ago
camAngles[YAW] += CAM_ANGLE_DELTA;
9 years ago
if( CL_KeyState( &cam_in ) )
{
dist -= CAM_DIST_DELTA;
if( dist < CAM_MIN_DIST )
{
// If we go back into first person, reset the angle
9 years ago
camAngles[PITCH] = 0;
camAngles[YAW] = 0;
9 years ago
dist = CAM_MIN_DIST;
}
}
else if( CL_KeyState( &cam_out ) )
dist += CAM_DIST_DELTA;
9 years ago
if( cam_distancemove )
9 years ago
{
9 years ago
if( cam_mouse.y > gEngfuncs.GetWindowCenterY() )
9 years ago
{
9 years ago
if( dist < c_maxdistance->value )
9 years ago
{
9 years ago
dist += CAM_DIST_DELTA * ( ( cam_mouse.y - gEngfuncs.GetWindowCenterY() ) / 2);
9 years ago
}
9 years ago
if( dist > c_maxdistance->value )
9 years ago
{
9 years ago
dist = c_maxdistance->value;
9 years ago
}
}
9 years ago
else if( cam_mouse.y < gEngfuncs.GetWindowCenterY() )
9 years ago
{
9 years ago
if( dist > c_mindistance->value )
9 years ago
{
9 years ago
dist -= CAM_DIST_DELTA * ( ( gEngfuncs.GetWindowCenterY() - cam_mouse.y ) / 2 );
9 years ago
}
9 years ago
if ( dist < c_mindistance->value )
9 years ago
{
9 years ago
dist = c_mindistance->value;
9 years ago
}
}
//set old mouse coordinates to current mouse coordinates
//since we are done with the mouse
9 years ago
cam_old_mouse_x = cam_mouse.x * gHUD.GetSensitivity();
cam_old_mouse_y = cam_mouse.y * gHUD.GetSensitivity();
SetCursorPos( gEngfuncs.GetWindowCenterX(), gEngfuncs.GetWindowCenterY() );
9 years ago
}
#ifdef LATER
if( cam_contain->value )
{
// check new ideal
VectorCopy( origin, pnt );
AngleVectors( camAngles, camForward, camRight, camUp );
9 years ago
for( i = 0; i < 3; i++ )
pnt[i] += -dist * camForward[i];
9 years ago
// check line from r_refdef.vieworg to pnt
9 years ago
memset( &clip, 0, sizeof(moveclip_t) );
9 years ago
clip.trace = SV_ClipMoveToEntity( sv.edicts, r_refdef.vieworg, ext, ext, pnt );
if( clip.trace.fraction == 1.0 )
{
// update ideal
9 years ago
cam_idealpitch->value = camAngles[PITCH];
cam_idealyaw->value = camAngles[YAW];
9 years ago
cam_idealdist->value = dist;
}
}
else
#endif
{
// update ideal
9 years ago
cam_idealpitch->value = camAngles[PITCH];
cam_idealyaw->value = camAngles[YAW];
9 years ago
cam_idealdist->value = dist;
}
// Move towards ideal
VectorCopy( cam_ofs, camAngles );
gEngfuncs.GetViewAngles( (float *)viewangles );
if( cam_snapto->value )
{
9 years ago
camAngles[YAW] = cam_idealyaw->value + viewangles[YAW];
camAngles[PITCH] = cam_idealpitch->value + viewangles[PITCH];
camAngles[2] = cam_idealdist->value;
9 years ago
}
else
{
9 years ago
if( camAngles[YAW] - viewangles[YAW] != cam_idealyaw->value )
camAngles[YAW] = MoveToward( camAngles[YAW], cam_idealyaw->value + viewangles[YAW], CAM_ANGLE_SPEED );
9 years ago
9 years ago
if( camAngles[PITCH] - viewangles[PITCH] != cam_idealpitch->value )
camAngles[PITCH] = MoveToward( camAngles[PITCH], cam_idealpitch->value + viewangles[PITCH], CAM_ANGLE_SPEED );
9 years ago
if( fabs( camAngles[2] - cam_idealdist->value ) < 2.0f )
9 years ago
camAngles[2] = cam_idealdist->value;
9 years ago
else
camAngles[2] += ( cam_idealdist->value - camAngles[2] ) * 0.25f;
9 years ago
}
#ifdef LATER
if( cam_contain->value )
{
// Test new position
9 years ago
dist = camAngles[ROLL];
camAngles[ROLL] = 0;
9 years ago
VectorCopy( origin, pnt );
AngleVectors( camAngles, camForward, camRight, camUp );
9 years ago
for( i = 0; i < 3; i++ )
pnt[i] += -dist * camForward[i];
9 years ago
// check line from r_refdef.vieworg to pnt
9 years ago
memset( &clip, 0, sizeof(moveclip_t) );
ext[0] = ext[1] = ext[2] = 0.0f;
9 years ago
clip.trace = SV_ClipMoveToEntity( sv.edicts, r_refdef.vieworg, ext, ext, pnt );
if( clip.trace.fraction != 1.0f )
9 years ago
return;
}
#endif
9 years ago
cam_ofs[0] = camAngles[0];
cam_ofs[1] = camAngles[1];
cam_ofs[2] = dist;
9 years ago
}
9 years ago
extern void KeyDown( kbutton_t *b ); // HACK
extern void KeyUp( kbutton_t *b ); // HACK
void CAM_PitchUpDown( void )
{
KeyDown( &cam_pitchup );
}
9 years ago
9 years ago
void CAM_PitchUpUp( void )
{
KeyUp( &cam_pitchup );
}
void CAM_PitchDownDown( void )
{
KeyDown( &cam_pitchdown );
}
void CAM_PitchDownUp( void )
{
KeyUp( &cam_pitchdown );
}
void CAM_YawLeftDown( void )
{
KeyDown( &cam_yawleft );
}
void CAM_YawLeftUp( void )
{
KeyUp( &cam_yawleft );
}
void CAM_YawRightDown( void )
{
KeyDown( &cam_yawright );
}
void CAM_YawRightUp( void )
{
KeyUp( &cam_yawright );
}
void CAM_InDown( void )
{
KeyDown( &cam_in );
}
void CAM_InUp( void )
{
KeyUp( &cam_in );
}
void CAM_OutDown( void )
{
KeyDown( &cam_out );
}
void CAM_OutUp( void )
{
KeyUp( &cam_out );
}
void CAM_ToThirdPerson( void )
{
vec3_t viewangles;
9 years ago
#if !defined( _DEBUG )
9 years ago
if( gEngfuncs.GetMaxClients() > 1 )
9 years ago
{
// no thirdperson in multiplayer.
return;
}
#endif
gEngfuncs.GetViewAngles( (float *)viewangles );
if( !cam_thirdperson )
{
cam_thirdperson = 1;
9 years ago
cam_ofs[YAW] = viewangles[YAW];
cam_ofs[PITCH] = viewangles[PITCH];
cam_ofs[2] = CAM_MIN_DIST;
9 years ago
}
gEngfuncs.Cvar_SetValue( "cam_command", 0 );
}
9 years ago
void CAM_ToFirstPerson( void )
9 years ago
{
cam_thirdperson = 0;
gEngfuncs.Cvar_SetValue( "cam_command", 0 );
}
void CAM_ToggleSnapto( void )
{
cam_snapto->value = !cam_snapto->value;
}
void CAM_Init( void )
{
gEngfuncs.pfnAddCommand( "+campitchup", CAM_PitchUpDown );
gEngfuncs.pfnAddCommand( "-campitchup", CAM_PitchUpUp );
gEngfuncs.pfnAddCommand( "+campitchdown", CAM_PitchDownDown );
gEngfuncs.pfnAddCommand( "-campitchdown", CAM_PitchDownUp );
gEngfuncs.pfnAddCommand( "+camyawleft", CAM_YawLeftDown );
gEngfuncs.pfnAddCommand( "-camyawleft", CAM_YawLeftUp );
gEngfuncs.pfnAddCommand( "+camyawright", CAM_YawRightDown );
gEngfuncs.pfnAddCommand( "-camyawright", CAM_YawRightUp );
gEngfuncs.pfnAddCommand( "+camin", CAM_InDown );
gEngfuncs.pfnAddCommand( "-camin", CAM_InUp );
gEngfuncs.pfnAddCommand( "+camout", CAM_OutDown );
gEngfuncs.pfnAddCommand( "-camout", CAM_OutUp );
gEngfuncs.pfnAddCommand( "thirdperson", CAM_ToThirdPerson );
gEngfuncs.pfnAddCommand( "firstperson", CAM_ToFirstPerson );
gEngfuncs.pfnAddCommand( "+cammousemove",CAM_StartMouseMove);
gEngfuncs.pfnAddCommand( "-cammousemove",CAM_EndMouseMove);
gEngfuncs.pfnAddCommand( "+camdistance", CAM_StartDistance );
gEngfuncs.pfnAddCommand( "-camdistance", CAM_EndDistance );
gEngfuncs.pfnAddCommand( "snapto", CAM_ToggleSnapto );
9 years ago
cam_command = gEngfuncs.pfnRegisterVariable( "cam_command", "0", 0 ); // tells camera to go to thirdperson
cam_snapto = gEngfuncs.pfnRegisterVariable( "cam_snapto", "0", 0 ); // snap to thirdperson view
cam_idealyaw = gEngfuncs.pfnRegisterVariable( "cam_idealyaw", "90", 0 ); // thirdperson yaw
cam_idealpitch = gEngfuncs.pfnRegisterVariable( "cam_idealpitch", "0", 0 ); // thirperson pitch
cam_idealdist = gEngfuncs.pfnRegisterVariable( "cam_idealdist", "64", 0 ); // thirdperson distance
cam_contain = gEngfuncs.pfnRegisterVariable( "cam_contain", "0", 0 ); // contain camera to world
c_maxpitch = gEngfuncs.pfnRegisterVariable( "c_maxpitch", "90.0", 0 );
c_minpitch = gEngfuncs.pfnRegisterVariable( "c_minpitch", "0.0", 0 );
c_maxyaw = gEngfuncs.pfnRegisterVariable( "c_maxyaw", "135.0", 0 );
c_minyaw = gEngfuncs.pfnRegisterVariable( "c_minyaw", "-135.0", 0 );
c_maxdistance = gEngfuncs.pfnRegisterVariable( "c_maxdistance", "200.0", 0 );
c_mindistance = gEngfuncs.pfnRegisterVariable( "c_mindistance", "30.0", 0 );
9 years ago
}
void CAM_ClearStates( void )
{
vec3_t viewangles;
gEngfuncs.GetViewAngles( (float *)viewangles );
cam_pitchup.state = 0;
cam_pitchdown.state = 0;
cam_yawleft.state = 0;
cam_yawright.state = 0;
cam_in.state = 0;
cam_out.state = 0;
cam_thirdperson = 0;
cam_command->value = 0;
cam_mousemove=0;
cam_snapto->value = 0;
cam_distancemove = 0;
9 years ago
cam_ofs[0] = 0.0;
cam_ofs[1] = 0.0;
cam_ofs[2] = CAM_MIN_DIST;
9 years ago
9 years ago
cam_idealpitch->value = viewangles[PITCH];
cam_idealyaw->value = viewangles[YAW];
9 years ago
cam_idealdist->value = CAM_MIN_DIST;
}
9 years ago
void CAM_StartMouseMove( void )
9 years ago
{
float flSensitivity;
9 years ago
9 years ago
//only move the cam with mouse if we are in third person.
9 years ago
if( cam_thirdperson )
9 years ago
{
//set appropriate flags and initialize the old mouse position
//variables for mouse camera movement
9 years ago
if( !cam_mousemove )
9 years ago
{
9 years ago
cam_mousemove = 1;
iMouseInUse = 1;
GetCursorPos( &cam_mouse );
9 years ago
9 years ago
if( ( flSensitivity = gHUD.GetSensitivity() ) != 0 )
9 years ago
{
9 years ago
cam_old_mouse_x = cam_mouse.x * flSensitivity;
cam_old_mouse_y = cam_mouse.y * flSensitivity;
9 years ago
}
else
{
9 years ago
cam_old_mouse_x = cam_mouse.x;
cam_old_mouse_y = cam_mouse.y;
9 years ago
}
}
}
//we are not in 3rd person view..therefore do not allow camera movement
else
{
9 years ago
cam_mousemove = 0;
iMouseInUse = 0;
9 years ago
}
}
//the key has been released for camera movement
//tell the engine that mouse camera movement is off
9 years ago
void CAM_EndMouseMove( void )
9 years ago
{
9 years ago
cam_mousemove = 0;
iMouseInUse = 0;
9 years ago
}
//----------------------------------------------------------
//routines to start the process of moving the cam in or out
//using the mouse
//----------------------------------------------------------
9 years ago
void CAM_StartDistance( void )
9 years ago
{
//only move the cam with mouse if we are in third person.
9 years ago
if( cam_thirdperson )
9 years ago
{
9 years ago
//set appropriate flags and initialize the old mouse position
//variables for mouse camera movement
if( !cam_distancemove )
{
cam_distancemove = 1;
cam_mousemove = 1;
iMouseInUse = 1;
GetCursorPos( &cam_mouse );
cam_old_mouse_x = cam_mouse.x * gHUD.GetSensitivity();
cam_old_mouse_y = cam_mouse.y * gHUD.GetSensitivity();
}
9 years ago
}
//we are not in 3rd person view..therefore do not allow camera movement
else
9 years ago
{
cam_distancemove = 0;
cam_mousemove = 0;
iMouseInUse = 0;
9 years ago
}
}
//the key has been released for camera movement
//tell the engine that mouse camera movement is off
9 years ago
void CAM_EndDistance( void )
9 years ago
{
9 years ago
cam_distancemove = 0;
cam_mousemove = 0;
iMouseInUse = 0;
9 years ago
}
int DLLEXPORT CL_IsThirdPerson( void )
{
return cam_thirdperson ? 1 : 0;
9 years ago
}
void DLLEXPORT CL_CameraOffset( float *ofs )
{
VectorCopy( cam_ofs, ofs );
}