Skip to content

Commit 7be4d00

Browse files
committed
send controller positions to server
1 parent 2d1c0ea commit 7be4d00

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

L4D2VR/game.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22
#include <cstdint>
33
#include <array>
4-
#include <QAngle.h>
4+
#include "QAngle.h"
5+
#include "vector.h"
56

67
class IClientEntityList;
78
class IEngineTrace;
@@ -19,7 +20,8 @@ class Hooks;
1920
struct Player
2021
{
2122
bool isUsingVR;
22-
QAngle controllerAngles;
23+
QAngle controllerAngle;
24+
Vector controllerPos;
2325
};
2426

2527
class Game

L4D2VR/hooks.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ int Hooks::dServerFireTerrorBullets(int playerId, const Vector &vecOrigin, const
328328
// Clients
329329
else if (mGame->playersVRInfo[playerId].isUsingVR)
330330
{
331-
vecNewAngles = mGame->playersVRInfo[playerId].controllerAngles;
331+
vecNewOrigin = mGame->playersVRInfo[playerId].controllerPos;
332+
vecNewAngles = mGame->playersVRInfo[playerId].controllerAngle;
332333
}
333334

334335
return hkServerFireTerrorBullets.fOriginal(playerId, vecNewOrigin, vecNewAngles, a4, a5, a6, a7);
@@ -368,11 +369,27 @@ int Hooks::dReadUsercmd(void *buf, CUserCmd *move, CUserCmd *from)
368369
hkReadUsercmd.fOriginal(buf, move, from);
369370

370371
int i = mGame->currentUsercmdID;
371-
if (move->viewangles.z == -1.0) // Signal for VR CUserCmd
372+
if (move->tick_count < 0) // Signal for VR CUserCmd
372373
{
374+
move->tick_count *= -1;
375+
373376
mGame->playersVRInfo[i].isUsingVR = true;
374-
mGame->playersVRInfo[i].controllerAngles.x = (float)move->mousedx / 10;
375-
mGame->playersVRInfo[i].controllerAngles.y = (float)move->mousedy / 10;
377+
mGame->playersVRInfo[i].controllerAngle.x = (float)move->mousedx / 10;
378+
mGame->playersVRInfo[i].controllerAngle.y = (float)move->mousedy / 10;
379+
mGame->playersVRInfo[i].controllerPos.x = move->viewangles.z;
380+
mGame->playersVRInfo[i].controllerPos.y = move->upmove;
381+
382+
// Decode viewangles.x
383+
int decodedZInt = (move->viewangles.x / 10000);
384+
float decodedAngle = abs((float)(move->viewangles.x - (decodedZInt * 10000)) / 10);
385+
decodedAngle -= 360;
386+
float decodedZ = (float)decodedZInt / 10;
387+
388+
mGame->playersVRInfo[i].controllerPos.z = decodedZ;
389+
390+
move->viewangles.x = decodedAngle;
391+
move->viewangles.z = 0;
392+
move->upmove = 0;
376393
}
377394
else
378395
{
@@ -395,16 +412,37 @@ int Hooks::dWriteUsercmd(void *buf, CUserCmd *to, CUserCmd *from)
395412
CVerifiedUserCmd *pVerified = &pVerifiedCommands[(to->command_number) % 150];
396413

397414
// Signal to the server that this CUserCmd has VR info
398-
to->viewangles.z = -1;
415+
to->tick_count *= -1;
399416

400417
QAngle controllerAngles = mVR->GetRecommendedViewmodelAbsAngle();
401418
to->mousedx = controllerAngles.x * 10; // Strip off 2nd decimal to save bits.
402419
to->mousedy = controllerAngles.y * 10;
403420

421+
Vector controllerPos = mVR->GetRecommendedViewmodelAbsPos();
422+
to->viewangles.z = controllerPos.x;
423+
to->upmove = controllerPos.y;
424+
425+
// Space in CUserCmd is tight, so encode viewangle.x and controllerPos.z together.
426+
// Encoding will overflow if controllerPos.z goes beyond +-21474.8
427+
float xAngle = to->viewangles.x;
428+
int encodedAngle = (xAngle + 360) * 10;
429+
int encoding = (int)(controllerPos.z * 10) * 10000;
430+
encoding += encoding < 0 ? -encodedAngle : encodedAngle;
431+
to->viewangles.x = encoding;
432+
433+
hkWriteUsercmd.fOriginal(buf, to, from);
434+
435+
to->viewangles.x = xAngle;
436+
to->tick_count *= -1;
437+
to->viewangles.z = 0;
438+
to->upmove = 0;
439+
404440
// Must recalculate checksum for the edited CUserCmd or gunshots will sound
405441
// terrible in multiplayer.
406442
pVerified->m_cmd = *to;
407443
pVerified->m_crc = to->GetChecksum();
444+
return 1;
445+
408446
}
409447
return hkWriteUsercmd.fOriginal(buf, to, from);
410448
}

L4D2VR/vr.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,16 @@ void VR::ProcessInput()
250250
// Smooth turning
251251
else
252252
{
253-
if (analogActionData.x > 0.2)
253+
float deadzone = 0.2;
254+
// smoother turning
255+
float xNormalized = (abs(analogActionData.x) - deadzone) / (1 - deadzone);
256+
if (analogActionData.x > deadzone)
254257
{
255-
mRotationOffset -= mTurnSpeed * deltaTime * analogActionData.x;
258+
mRotationOffset -= mTurnSpeed * deltaTime * xNormalized;
256259
}
257-
if (analogActionData.x < -0.2)
260+
if (analogActionData.x < -deadzone)
258261
{
259-
mRotationOffset += mTurnSpeed * deltaTime * (-analogActionData.x);
262+
mRotationOffset += mTurnSpeed * deltaTime * xNormalized;
260263
}
261264
}
262265

0 commit comments

Comments
 (0)