2020-06-10 18:20:11 +00:00
/************************************************************************************
Filename : VrInputDefault . c
Content : Handles default controller input
Created : August 2019
Authors : Simon Brown
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <VrApi.h>
# include <VrApi_Helpers.h>
# include <VrApi_SystemUtils.h>
# include <VrApi_Input.h>
# include <VrApi_Types.h>
# include "VrInput.h"
# include "VrCvars.h"
2020-06-28 13:41:34 +00:00
# include "../rtcw/src/client/client.h"
2020-06-10 18:20:11 +00:00
cvar_t * sv_cheats ;
extern cvar_t * vr_weapon_stabilised ;
2020-06-30 23:03:19 +00:00
2020-06-10 18:20:11 +00:00
void HandleInput_Default ( ovrInputStateTrackedRemote * pDominantTrackedRemoteNew , ovrInputStateTrackedRemote * pDominantTrackedRemoteOld , ovrTracking * pDominantTracking ,
ovrInputStateTrackedRemote * pOffTrackedRemoteNew , ovrInputStateTrackedRemote * pOffTrackedRemoteOld , ovrTracking * pOffTracking ,
int domButton1 , int domButton2 , int offButton1 , int offButton2 )
{
//Ensure handedness is set correctly
Cvar_Set ( " hand " , vr_control_scheme - > value < 10 ? " 0 " : " 1 " ) ;
//Get the cvar
2020-07-01 22:49:41 +00:00
sv_cheats = Cvar_Get ( " cheats " , " 1 " , CVAR_ARCHIVE ) ;
2020-06-10 18:20:11 +00:00
static qboolean dominantGripPushed = false ;
static float dominantGripPushTime = 0.0f ;
2020-07-01 22:49:41 +00:00
static int grabMeleeWeapon = 0 ;
static bool canUseBackpack = false ;
2020-06-10 18:20:11 +00:00
2020-07-01 22:49:41 +00:00
//switch to screen layer override
if ( ( pOffTrackedRemoteNew - > Buttons & ovrButton_Joystick ) ! =
( pOffTrackedRemoteOld - > Buttons & ovrButton_Joystick ) ) {
showingScreenLayer = ! showingScreenLayer ;
}
//Menu button
2020-06-10 18:20:11 +00:00
handleTrackedControllerButton ( & leftTrackedRemoteState_new , & leftTrackedRemoteState_old , ovrButton_Enter , K_ESCAPE ) ;
2020-07-01 22:49:41 +00:00
if ( RTCWVR_useScreenLayer ( ) )
2020-06-10 18:20:11 +00:00
{
2020-06-30 23:03:19 +00:00
interactWithTouchScreen ( pDominantTracking , pDominantTrackedRemoteNew , pDominantTrackedRemoteOld ) ;
2020-06-10 18:20:11 +00:00
2020-06-30 23:03:19 +00:00
handleTrackedControllerButton ( pDominantTrackedRemoteNew , pDominantTrackedRemoteOld , domButton1 , K_MOUSE1 ) ;
handleTrackedControllerButton ( pDominantTrackedRemoteNew , pDominantTrackedRemoteOld , ovrButton_Trigger , K_MOUSE1 ) ;
2020-06-10 18:20:11 +00:00
handleTrackedControllerButton ( pDominantTrackedRemoteNew , pDominantTrackedRemoteOld , domButton2 , K_ESCAPE ) ;
}
2020-06-30 23:03:19 +00:00
else
2020-06-10 18:20:11 +00:00
{
float distance = sqrtf ( powf ( pOffTracking - > HeadPose . Pose . Position . x - pDominantTracking - > HeadPose . Pose . Position . x , 2 ) +
powf ( pOffTracking - > HeadPose . Pose . Position . y - pDominantTracking - > HeadPose . Pose . Position . y , 2 ) +
powf ( pOffTracking - > HeadPose . Pose . Position . z - pDominantTracking - > HeadPose . Pose . Position . z , 2 ) ) ;
//Turn on weapon stabilisation?
if ( ( pOffTrackedRemoteNew - > Buttons & ovrButton_GripTrigger ) ! =
( pOffTrackedRemoteOld - > Buttons & ovrButton_GripTrigger ) ) {
if ( pOffTrackedRemoteNew - > Buttons & ovrButton_GripTrigger )
{
if ( distance < 0.50f )
{
2020-06-30 23:03:19 +00:00
Cvar_Set ( " vr_weapon_stabilised " , " 1.0 " ) ;
2020-06-10 18:20:11 +00:00
}
}
else
{
2020-06-30 23:03:19 +00:00
Cvar_Set ( " vr_weapon_stabilised " , " 0.0 " ) ;
2020-06-10 18:20:11 +00:00
}
}
//dominant hand stuff first
{
///Weapon location relative to view
weaponoffset [ 0 ] = pDominantTracking - > HeadPose . Pose . Position . x - hmdPosition [ 0 ] ;
weaponoffset [ 1 ] = pDominantTracking - > HeadPose . Pose . Position . y - hmdPosition [ 1 ] ;
weaponoffset [ 2 ] = pDominantTracking - > HeadPose . Pose . Position . z - hmdPosition [ 2 ] ;
{
vec2_t v ;
2020-06-28 13:41:34 +00:00
rotateAboutOrigin ( - weaponoffset [ 0 ] , weaponoffset [ 2 ] , ( cl . viewangles [ YAW ] - hmdorientation [ YAW ] ) , v ) ;
2020-06-10 18:20:11 +00:00
weaponoffset [ 0 ] = v [ 0 ] ;
weaponoffset [ 2 ] = v [ 1 ] ;
}
//Set gun angles - We need to calculate all those we might need (including adjustments) for the client to then take its pick
2020-06-30 23:03:19 +00:00
vec3_t rotation = { 0 } ;
rotation [ PITCH ] = vr_weapon_pitchadjust - > value ;
2020-07-01 22:49:41 +00:00
QuatToYawPitchRoll ( pDominantTracking - > HeadPose . Pose . Orientation , rotation , weaponangles ) ;
2020-06-28 13:41:34 +00:00
weaponangles [ YAW ] + = ( cl . viewangles [ YAW ] - hmdorientation [ YAW ] ) ;
2020-06-10 18:20:11 +00:00
weaponangles [ ROLL ] * = - 1.0f ;
if ( vr_weapon_stabilised - > value = = 1.0f )
{
float z = pOffTracking - > HeadPose . Pose . Position . z - pDominantTracking - > HeadPose . Pose . Position . z ;
float x = pOffTracking - > HeadPose . Pose . Position . x - pDominantTracking - > HeadPose . Pose . Position . x ;
float y = pOffTracking - > HeadPose . Pose . Position . y - pDominantTracking - > HeadPose . Pose . Position . y ;
float zxDist = length ( x , z ) ;
if ( zxDist ! = 0.0f & & z ! = 0.0f ) {
2020-06-28 13:41:34 +00:00
VectorSet ( weaponangles , - degrees ( atanf ( y / zxDist ) ) , ( cl . viewangles [ YAW ] - hmdorientation [ YAW ] ) - degrees ( atan2f ( x , - z ) ) , weaponangles [ ROLL ] ) ;
2020-06-10 18:20:11 +00:00
}
}
2020-07-01 22:49:41 +00:00
static bool finishReloadNextFrame = false ;
if ( finishReloadNextFrame )
{
sendButtonActionSimple ( " -reload " ) ;
finishReloadNextFrame = false ;
}
if ( pDominantTracking - > Status & ( VRAPI_TRACKING_STATUS_POSITION_TRACKED | VRAPI_TRACKING_STATUS_POSITION_VALID ) ) {
canUseBackpack = false ;
}
else if ( ! canUseBackpack & & grabMeleeWeapon = = 0 ) {
int channel = ( vr_control_scheme - > integer > = 10 ) ? 0 : 1 ;
RTCWVR_Vibrate ( 40 , channel ,
0.5 ) ; // vibrate to let user know they can switch
canUseBackpack = true ;
}
2020-06-10 18:20:11 +00:00
if ( ( pDominantTrackedRemoteNew - > Buttons & ovrButton_GripTrigger ) ! =
( pDominantTrackedRemoteOld - > Buttons & ovrButton_GripTrigger ) ) {
2020-07-01 22:49:41 +00:00
dominantGripPushed = ( pDominantTrackedRemoteNew - > Buttons &
ovrButton_GripTrigger ) ! = 0 ;
if ( grabMeleeWeapon = = 0 )
{
if ( pDominantTracking - > Status & ( VRAPI_TRACKING_STATUS_POSITION_TRACKED | VRAPI_TRACKING_STATUS_POSITION_VALID ) ) {
if ( dominantGripPushed ) {
dominantGripPushTime = GetTimeInMilliSeconds ( ) ;
} else {
if ( ( GetTimeInMilliSeconds ( ) - dominantGripPushTime ) <
vr_reloadtimeoutms - > integer ) {
sendButtonActionSimple ( " +reload " ) ;
finishReloadNextFrame = true ;
}
}
} else {
if ( dominantGripPushed ) {
//Initiate crowbar from backpack mode
sendButtonActionSimple ( " weapon0 " ) ;
int channel = ( vr_control_scheme - > integer > = 10 ) ? 0 : 1 ;
RTCWVR_Vibrate ( 80 , channel , 0.8 ) ; // vibrate to let user know they switched
grabMeleeWeapon = 1 ;
}
}
} else if ( grabMeleeWeapon = = 1 & & ! dominantGripPushed ) {
//Restores last used weapon
sendButtonActionSimple ( " lastinv " ) ;
grabMeleeWeapon = 0 ;
}
2020-06-10 18:20:11 +00:00
}
}
float controllerYawHeading = 0.0f ;
//off-hand stuff
{
flashlightoffset [ 0 ] = pOffTracking - > HeadPose . Pose . Position . x - hmdPosition [ 0 ] ;
flashlightoffset [ 1 ] = pOffTracking - > HeadPose . Pose . Position . y - hmdPosition [ 1 ] ;
flashlightoffset [ 2 ] = pOffTracking - > HeadPose . Pose . Position . z - hmdPosition [ 2 ] ;
vec2_t v ;
2020-06-28 13:41:34 +00:00
rotateAboutOrigin ( - flashlightoffset [ 0 ] , flashlightoffset [ 2 ] , ( cl . viewangles [ YAW ] - hmdorientation [ YAW ] ) , v ) ;
2020-06-10 18:20:11 +00:00
flashlightoffset [ 0 ] = v [ 0 ] ;
flashlightoffset [ 2 ] = v [ 1 ] ;
2020-06-30 23:03:19 +00:00
vec3_t rotation = { 0 } ;
QuatToYawPitchRoll ( pOffTracking - > HeadPose . Pose . Orientation , rotation , flashlightangles ) ;
2020-06-10 18:20:11 +00:00
2020-06-28 13:41:34 +00:00
flashlightangles [ YAW ] + = ( cl . viewangles [ YAW ] - hmdorientation [ YAW ] ) ;
2020-06-10 18:20:11 +00:00
if ( vr_walkdirection - > value = = 0 ) {
2020-06-28 13:41:34 +00:00
controllerYawHeading = - cl . viewangles [ YAW ] + flashlightangles [ YAW ] ;
2020-06-10 18:20:11 +00:00
}
else
{
controllerYawHeading = 0.0f ; //-cl.viewangles[YAW];
}
}
//Right-hand specific stuff
{
ALOGV ( " Right-Controller-Position: %f, %f, %f " ,
pDominantTracking - > HeadPose . Pose . Position . x ,
pDominantTracking - > HeadPose . Pose . Position . y ,
pDominantTracking - > HeadPose . Pose . Position . z ) ;
//This section corrects for the fact that the controller actually controls direction of movement, but we want to move relative to the direction the
//player is facing for positional tracking
vec2_t v ;
2020-07-01 22:49:41 +00:00
rotateAboutOrigin ( - positionDeltaThisFrame [ 0 ] * vr_positional_factor - > value ,
positionDeltaThisFrame [ 2 ] * vr_positional_factor - > value , - hmdorientation [ YAW ] , v ) ;
2020-06-10 18:20:11 +00:00
positional_movementSideways = v [ 0 ] ;
positional_movementForward = v [ 1 ] ;
ALOGV ( " positional_movementSideways: %f, positional_movementForward: %f " ,
positional_movementSideways ,
positional_movementForward ) ;
//Jump (B Button)
handleTrackedControllerButton ( pDominantTrackedRemoteNew ,
pDominantTrackedRemoteOld , domButton2 , K_SPACE ) ;
2020-07-01 22:49:41 +00:00
2020-06-10 18:20:11 +00:00
//We need to record if we have started firing primary so that releasing trigger will stop firing, if user has pushed grip
//in meantime, then it wouldn't stop the gun firing and it would get stuck
static bool firingPrimary = false ;
2020-07-01 22:49:41 +00:00
if ( ! firingPrimary & & dominantGripPushed & & ( GetTimeInMilliSeconds ( ) - dominantGripPushTime ) > vr_reloadtimeoutms - > integer )
{
//Fire Secondary
if ( ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Trigger ) ! =
( pDominantTrackedRemoteOld - > Buttons & ovrButton_Trigger ) ) {
2020-06-10 18:20:11 +00:00
2020-07-01 22:49:41 +00:00
sendButtonAction ( " +attack2 " , ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Trigger ) ) ;
}
}
else
{
//Fire Primary
if ( ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Trigger ) ! =
( pDominantTrackedRemoteOld - > Buttons & ovrButton_Trigger ) ) {
2020-06-10 18:20:11 +00:00
2020-07-01 22:49:41 +00:00
firingPrimary = ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Trigger ) ;
sendButtonAction ( " +attack " , firingPrimary ) ;
}
// we need to release secondary fire if dominantGripPushed has been released before releasing trigger -> should fix the gun jamming and non stop firing secondary attack bug
if ( ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Trigger ) ! =
( pDominantTrackedRemoteOld - > Buttons & ovrButton_Trigger ) & &
( pDominantTrackedRemoteNew - > Buttons & ovrButton_Trigger ) = = false )
{
sendButtonAction ( " +attack2 " , false ) ;
}
}
2020-06-10 18:20:11 +00:00
//Duck with A
if ( ( pDominantTrackedRemoteNew - > Buttons & domButton1 ) ! =
( pDominantTrackedRemoteOld - > Buttons & domButton1 ) & &
ducked ! = DUCK_CROUCHED ) {
ducked = ( pDominantTrackedRemoteNew - > Buttons & domButton1 ) ? DUCK_BUTTON : DUCK_NOTDUCKED ;
sendButtonAction ( " +movedown " , ( pDominantTrackedRemoteNew - > Buttons & domButton1 ) ) ;
}
//Weapon/Inventory Chooser
static qboolean itemSwitched = false ;
if ( between ( - 0.2f , pDominantTrackedRemoteNew - > Joystick . x , 0.2f ) & &
( between ( 0.8f , pDominantTrackedRemoteNew - > Joystick . y , 1.0f ) | |
between ( - 1.0f , pDominantTrackedRemoteNew - > Joystick . y , - 0.8f ) ) )
{
if ( ! itemSwitched ) {
if ( between ( 0.8f , pDominantTrackedRemoteNew - > Joystick . y , 1.0f ) )
{
2020-07-01 22:49:41 +00:00
sendButtonActionSimple ( " weapprev " ) ;
2020-06-10 18:20:11 +00:00
}
else
{
2020-07-01 22:49:41 +00:00
sendButtonActionSimple ( " weapnext " ) ;
2020-06-10 18:20:11 +00:00
}
itemSwitched = true ;
}
} else {
itemSwitched = false ;
}
}
//Left-hand specific stuff
{
ALOGV ( " Left-Controller-Position: %f, %f, %f " ,
pOffTracking - > HeadPose . Pose . Position . x ,
pOffTracking - > HeadPose . Pose . Position . y ,
pOffTracking - > HeadPose . Pose . Position . z ) ;
2020-07-01 22:49:41 +00:00
//"Use" (open doors etc)
if ( ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Joystick ) ! =
( pDominantTrackedRemoteOld - > Buttons & ovrButton_Joystick ) ) {
sendButtonAction ( " +activate " , ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Joystick ) ? 1 : 0 ) ;
}
//Laser-sight - not implemented
/*
2020-06-10 18:20:11 +00:00
if ( ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Joystick ) ! =
( pDominantTrackedRemoteOld - > Buttons & ovrButton_Joystick )
& & ( pDominantTrackedRemoteNew - > Buttons & ovrButton_Joystick ) ) {
if ( vr_lasersight - > value ! = 0.0 )
{
2020-06-28 13:41:34 +00:00
// Cvar_ForceSet("vr_lasersight", "0.0");
2020-06-10 18:20:11 +00:00
} else {
2020-06-28 13:41:34 +00:00
// Cvar_ForceSet("vr_lasersight", "1.0");
2020-06-10 18:20:11 +00:00
}
}
2020-07-01 22:49:41 +00:00
*/
2020-06-10 18:20:11 +00:00
//Apply a filter and quadratic scaler so small movements are easier to make
float dist = length ( pOffTrackedRemoteNew - > Joystick . x , pOffTrackedRemoteNew - > Joystick . y ) ;
float nlf = nonLinearFilter ( dist ) ;
float x = nlf * pOffTrackedRemoteNew - > Joystick . x ;
float y = nlf * pOffTrackedRemoteNew - > Joystick . y ;
player_moving = ( fabs ( x ) + fabs ( y ) ) > 0.05f ;
//Adjust to be off-hand controller oriented
vec2_t v ;
rotateAboutOrigin ( x , y , controllerYawHeading , v ) ;
remote_movementSideways = v [ 0 ] ;
remote_movementForward = v [ 1 ] ;
ALOGV ( " remote_movementSideways: %f, remote_movementForward: %f " ,
remote_movementSideways ,
remote_movementForward ) ;
2020-07-01 22:49:41 +00:00
//Kick!
2020-06-10 18:20:11 +00:00
if ( ( pOffTrackedRemoteNew - > Buttons & offButton1 ) ! =
( pOffTrackedRemoteOld - > Buttons & offButton1 ) ) {
2020-07-01 22:49:41 +00:00
sendButtonAction ( " +kick " , ( pOffTrackedRemoteNew - > Buttons & offButton1 ) ) ;
2020-06-10 18:20:11 +00:00
}
2020-07-01 22:49:41 +00:00
//notebook
if ( ( pOffTrackedRemoteNew - > Buttons & offButton2 ) ! =
( pOffTrackedRemoteOld - > Buttons & offButton2 ) ) {
sendButtonActionSimple ( " notebook " ) ;
}
2020-06-10 18:20:11 +00:00
2020-07-01 22:49:41 +00:00
if ( ( ( pOffTrackedRemoteNew - > Buttons & ovrButton_Joystick ) ! =
( pOffTrackedRemoteOld - > Buttons & ovrButton_Joystick ) )
2020-06-10 18:20:11 +00:00
& & ( pOffTrackedRemoteNew - > Buttons & ovrButton_Joystick ) ) {
//If cheats enabled, give all weapons/pickups to player
if ( sv_cheats - > value = = 1.0f ) {
Cbuf_AddText ( " give all \n " ) ;
}
}
//We need to record if we have started firing primary so that releasing trigger will stop definitely firing, if user has pushed grip
//in meantime, then it wouldn't stop the gun firing and it would get stuck
static bool firingPrimary = false ;
//Run
handleTrackedControllerButton ( pOffTrackedRemoteNew ,
pOffTrackedRemoteOld ,
ovrButton_Trigger , K_SHIFT ) ;
static int increaseSnap = true ;
if ( pDominantTrackedRemoteNew - > Joystick . x > 0.6f )
{
if ( increaseSnap )
{
snapTurn - = vr_snapturn_angle - > value ;
if ( vr_snapturn_angle - > value > 10.0f ) {
increaseSnap = false ;
}
if ( snapTurn < - 180.0f )
{
snapTurn + = 360.f ;
}
}
} else if ( pDominantTrackedRemoteNew - > Joystick . x < 0.4f ) {
increaseSnap = true ;
}
static int decreaseSnap = true ;
if ( pDominantTrackedRemoteNew - > Joystick . x < - 0.6f )
{
if ( decreaseSnap )
{
snapTurn + = vr_snapturn_angle - > value ;
//If snap turn configured for less than 10 degrees
if ( vr_snapturn_angle - > value > 10.0f ) {
decreaseSnap = false ;
}
if ( snapTurn > 180.0f )
{
snapTurn - = 360.f ;
}
}
} else if ( pDominantTrackedRemoteNew - > Joystick . x > - 0.4f )
{
decreaseSnap = true ;
}
}
}
//Save state
rightTrackedRemoteState_old = rightTrackedRemoteState_new ;
leftTrackedRemoteState_old = leftTrackedRemoteState_new ;
}