Skip to content

Commit

Permalink
Add new freelook mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaldaien authored Apr 22, 2017
1 parent c75fb25 commit b5cd574
Showing 1 changed file with 128 additions and 5 deletions.
133 changes: 128 additions & 5 deletions nier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <SpecialK/log.h>
#include <SpecialK/steam_api.h>

#include <SpecialK/input/input.h>
#include <SpecialK/hooks.h>
#include <SpecialK/core.h>
#include <process.h>
Expand All @@ -20,7 +21,7 @@
#include <atlbase.h>


#define FAR_VERSION_NUM L"0.5.4.2"
#define FAR_VERSION_NUM L"0.5.4.3"
#define FAR_VERSION_STR L"FAR v " FAR_VERSION_NUM

// Block until update finishes, otherwise the update dialog
Expand Down Expand Up @@ -72,6 +73,7 @@ sk::ParameterBool* far_accepted_license = nullptr;
sk::ParameterStringW* far_hudless_binding = nullptr;
sk::ParameterStringW* far_center_lock = nullptr;
sk::ParameterStringW* far_focus_lock = nullptr;
sk::ParameterStringW* far_free_look = nullptr;

struct {
bool enqueue = false; // Trigger a Steam screenshot
Expand All @@ -85,7 +87,21 @@ struct {
} static __FAR_HUDLESS;


typedef float vec3_t [3]; // X,Z,Y

struct far_cam_state_s {
SK_Keybind freelook_binding = {
"Toggle Camera Freelook Mode", L"Num 5",
false, false, false, VK_NUMPAD5
};

// Memory addresses courtesy of Idk31 and Smithfield
vec3_t* pCamera = (vec3_t *)0x1415EB950;
vec3_t* pLook = (vec3_t *)0x1415EB960;
float* pRoll = (float *) 0x1415EB990;

vec3_t fwd, right, up;

bool center_lock = false,
focus_lock = false;

Expand Down Expand Up @@ -114,6 +130,8 @@ struct far_cam_state_s {
}
} static far_cam;

bool __FAR_Freelook = false;


#include <unordered_set>
static std::unordered_set <ID3D11Texture2D *> far_title_textures;
Expand Down Expand Up @@ -534,6 +552,86 @@ SK_FAR_EndFrame (void)
__FAR_HUDLESS.clear--;
}
}

XINPUT_STATE state;
if (__FAR_Freelook && SK_XInput_PollController (0, &state))
{
#if 0
#define VEC3_NORM(v) sqrt( (v)[0]*(v)[0] + (v)[1]*(v)[1] + (v)[2]*(v)[2] )
#endif

float ddX = (state.Gamepad.sThumbLX + 1.0f) / 32768.0f;
float ddY = (state.Gamepad.sThumbLY + 1.0f) / 32768.0f;

float ddA = (state.Gamepad.sThumbRX + 1.0f) / 32768.0f;
float ddB = (state.Gamepad.sThumbRY + 1.0f) / 32768.0f;

vec3_t pos; pos [0] = (*far_cam.pCamera) [0]; pos [1] = (*far_cam.pCamera) [1]; pos [2] = (*far_cam.pCamera) [2];
vec3_t target; target [0] = (*far_cam.pLook) [0]; target [1] = (*far_cam.pLook) [1]; target [2] = (*far_cam.pLook) [2];

vec3_t diff; diff [0] = target [0] - pos [0];
diff [1] = target [1] - pos [1];
diff [2] = target [2] - pos [2];

float hypXY = sqrtf (diff [0] * diff [0] + diff [2] * diff [2]);

float dX, dY, dZ;

dX = ddX*diff [2]/hypXY;
dY = ddX*diff [0]/hypXY;

#if 0
if (ddZ != 0.0f)
{
float roll = far_cam.roll;
float cosRoll = cosf (roll);

dX = dX*cosRoll
dY = dY*cosRoll
dZ = ddX*sinf(roll)

(*far_cam.pLook)[1] = target [1] - dZ;
(*far_cam.pCamera)[1] = pos [1] - dZ;
}
#endif

(*far_cam.pLook) [0] = target [0] - dX;
(*far_cam.pLook) [2] = target [2] + dY;

(*far_cam.pCamera) [0] = pos [0] - dX;
(*far_cam.pCamera) [2] = pos [2] + dY;

pos [0] = (*far_cam.pCamera) [0]; pos [1] = (*far_cam.pCamera) [1]; pos [2] = (*far_cam.pCamera) [2];
target [0] = (*far_cam.pLook) [0]; target [1] = (*far_cam.pLook) [1]; target [2] = (*far_cam.pLook) [2];

diff [0] = target [0] - pos [0];
diff [1] = target [1] - pos [1];
diff [2] = target [2] - pos [2];

hypXY = sqrtf (diff [0] * diff [0] + diff [2] * diff [2]);
float hypZ = sqrtf (diff [1] * diff [1] + hypXY * hypXY);

#if 0
if useZ then
dX = flySpeed*Xdiff/hypZ
dY = flySpeed*Ydiff/hypZ
dZ = flySpeed*Zdiff/hypZ
if moveDir == "backward" then dZ = -dZ end
writeFloat(tarZAD, tarZ+dZ)
writeFloat(camZAD, camZ+dZ)
else
#endif
dX = ddY * diff [0] / hypXY;
dY = ddY * diff [2] / hypXY;
dZ = ddY * diff [1] / hypXY;


(*far_cam.pLook) [0] = target [0] + dX;
(*far_cam.pLook) [2] = target [2] + dY;

(*far_cam.pCamera) [0] = pos [0] + dX;
(*far_cam.pCamera) [2] = pos [2] + dY;
}
}


Expand Down Expand Up @@ -589,6 +687,10 @@ SK_FAR_PluginKeyPress (BOOL Control, BOOL Shift, BOOL Alt, BYTE vkCode)
SK_MakeKeyMask ( far_cam.focus_binding.vKey, far_cam.focus_binding.ctrl,
far_cam.focus_binding.shift, far_cam.focus_binding.alt );

const UINT uiToggleFreelookMask =
SK_MakeKeyMask ( far_cam.freelook_binding.vKey, far_cam.freelook_binding.ctrl,
far_cam.freelook_binding.shift, far_cam.freelook_binding.alt );

switch (uiMaskedKeyCode)
{
case SK_ControlShiftKey (VK_OEM_PERIOD):
Expand Down Expand Up @@ -639,6 +741,11 @@ SK_FAR_PluginKeyPress (BOOL Control, BOOL Shift, BOOL Alt, BYTE vkCode)
{
far_cam.toggleFocusLock ();
}

else if (uiMaskedKeyCode == uiToggleFreelookMask)
{
__FAR_Freelook = (! __FAR_Freelook);
}
} break;
}

Expand Down Expand Up @@ -1782,9 +1889,10 @@ typedef void (WINAPI *D3D11_DrawInstancedIndirect_pfn)(
return ret;
};

far_hudless_binding = LoadKeybind (&__FAR_HUDLESS.keybind, L"HUDFreeScreenshot");
far_center_lock = LoadKeybind (&far_cam.center_binding, L"ToggleCameraCenterLock");
far_focus_lock = LoadKeybind (&far_cam.focus_binding, L"ToggleCameraFocusLock");
far_hudless_binding = LoadKeybind (&__FAR_HUDLESS.keybind, L"HUDFreeScreenshot");
far_center_lock = LoadKeybind (&far_cam.center_binding, L"ToggleCameraCenterLock");
far_focus_lock = LoadKeybind (&far_cam.focus_binding, L"ToggleCameraFocusLock");
far_free_look = LoadKeybind (&far_cam.freelook_binding, L"ToggleCameraFreelook");



Expand Down Expand Up @@ -2092,7 +2200,10 @@ SK_FAR_ControlPanel (void)
auto Keybinding = [](SK_Keybind* binding, sk::ParameterStringW* param) ->
auto
{
if (ImGui::Selectable (SK_WideCharToUTF8 (binding->human_readable).c_str(), false))
std::string label = SK_WideCharToUTF8 (binding->human_readable) + "###";
label += binding->bind_name;

if (ImGui::Selectable (label.c_str (), false))
{
ImGui::OpenPopup (binding->bind_name);
}
Expand Down Expand Up @@ -2137,9 +2248,21 @@ SK_FAR_ControlPanel (void)
far_cam.toggleFocusLock ();
}


ImGui::SameLine ();
changed |= Keybinding (&far_cam.focus_binding, far_focus_lock);

ImGui::Checkbox ("Use Gamepad Freelook", &__FAR_Freelook);

ImGui::SameLine ();
changed |= Keybinding (&far_cam.freelook_binding, far_free_look);

ImGui::Separator ();

ImGui::Text ( "Origin: (%.3f, %.3f, %.3f) - Look: (%.3f,%.3f,%.3f",
((float *)far_cam.pCamera)[0], ((float *)far_cam.pCamera)[1], ((float *)far_cam.pCamera)[2],
((float *)far_cam.pLook)[0], ((float *)far_cam.pLook)[1], ((float *)far_cam.pLook)[2] );

ImGui::TreePop ();
}

Expand Down

0 comments on commit b5cd574

Please sign in to comment.