diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 0000000..d2ff733 --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1,5 @@ +# Repository-relative exclusions for shared FormatValidation. +# Only src/, include/, and tests/ are selected; dependencies and artifacts are excluded. +**/thirdparty/** +**/build/** +**/install/** diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 0000000..e7736b0 --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,31 @@ +name: clang-format + +on: + push: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: clang-format-${{ github.ref }} + cancel-in-progress: true + +jobs: + clang-format: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install pinned formatter + run: python -m pip install clang-format==23.1.3 + - name: Configure formatting targets + run: cmake -S . -B build/format -DFORMAT_VALIDATION_ONLY=ON + - name: Check all owned C/C++ sources + run: cmake --build build/format --target format-check diff --git a/.gitignore b/.gitignore index 81c7fe9..376022b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ /thirdparty/cache/ *.user __pycache__/ + +# Generated from the shared FormatValidation source during CMake configuration. +/.clang-format diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c7e58c..ed25735 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,10 @@ cmake_minimum_required(VERSION 3.21) +option(FORMAT_VALIDATION_ONLY "Configure only clang-format targets without native dependencies" OFF) +if(FORMAT_VALIDATION_ONLY) + project(FormatValidation LANGUAGES NONE) + include(cmake/FormatValidation.cmake) + return() +endif() cmake_policy(SET CMP0091 NEW) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") project(SCCameraFix LANGUAGES CXX) @@ -89,3 +95,6 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) metahooksv_add_launch_game() endif() endif() + +# Shared formatting targets are explicit; ordinary native builds do not run them. +include(cmake/FormatValidation.cmake) diff --git a/README.md b/README.md index a53d34c..e2c3ad9 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,36 @@ scripts\build-SCCameraFix-x86-Release.bat -DMETAHOOK_SOURCE_PATH=D:\MetaHook The path is the repository root that provides `include/metahook.h`, `include/HLSDK` and `include/Interface`. SCCameraFix uses no Capstone and no SourceSDK headers; the MetaHook SDK tree is its only dependency besides VC-LTL. Pass `-DSCCAMERAFIX_SYNC_GAMEDATA=OFF` to build without downloading gamedata. + +## C/C++ formatting + +Formatting uses [MetaHookSv/FormatValidation](https://github.com/MetaHookSv/FormatValidation) +and clang-format **23.1.3**, with the DiligentCore style (4 spaces, preserved include +order). Install the formatter for the Python interpreter used by CMake: + +```sh +python -m pip install clang-format==23.1.3 +cmake -S . -B build/format "-DFORMAT_VALIDATION_ONLY=ON" +cmake --build build/format --target format-check +cmake --build build/format --target format +``` + +The format-only configuration needs CMake 3.21+, Git, Python 3.9+ (CI uses 3.12), +and a build generator; `-G Ninja` works without Visual Studio. It prepares no native +SDK or game dependencies. Formatting targets are explicit and are not part of a +normal DLL build. With a Visual Studio generator, add `--config Debug` or +`--config Release` when building a formatting target. + +The aggregate provides `FORMAT_VALIDATION_SOURCE_PATH=thirdparty/FormatValidation`. +Standalone components accept that CMake variable or its environment counterpart; +if empty, FetchContent downloads the fixed tooling commit. Quote relative paths, +for example `"-DFORMAT_VALIDATION_SOURCE_PATH=../../thirdparty/FormatValidation"`. +Configuration generates the ignored root `.clang-format` for editors; change the +shared style rather than that generated copy. An optional +`FORMAT_VALIDATION_CLANG_FORMAT_EXECUTABLE` selects an explicit formatter, whose +version must still match the pin. + +Checks cover owned C/C++ files in `src/`, `include/`, and `tests/`, including +non-ignored new files. Repository-relative exclusions live in `.clang-format-ignore`. +Third-party sources and build artifacts are excluded. The `clang-format` workflow +checks the full scope on pushes, pull requests, and manual runs. diff --git a/cmake/FormatValidation.cmake b/cmake/FormatValidation.cmake new file mode 100644 index 0000000..2a630e4 --- /dev/null +++ b/cmake/FormatValidation.cmake @@ -0,0 +1,26 @@ +# Shared tool implementation; only this dependency bootstrap belongs to the consumer. +if(NOT DEFINED FORMAT_VALIDATION_SOURCE_PATH) + set(FORMAT_VALIDATION_SOURCE_PATH "$ENV{FORMAT_VALIDATION_SOURCE_PATH}" CACHE PATH + "FormatValidation source tree; empty fetches the pinned tooling") +endif() +if(FORMAT_VALIDATION_SOURCE_PATH) + get_filename_component(_format_validation_source "${FORMAT_VALIDATION_SOURCE_PATH}" + ABSOLUTE BASE_DIR "${CMAKE_CURRENT_LIST_DIR}/..") +else() + include(FetchContent) + FetchContent_Declare(sccamerafix_format_validation + GIT_REPOSITORY https://github.com/MetaHookSv/FormatValidation.git + GIT_TAG 13c9fabe058e1f887ad1b03bb6884de911192c6a # v1.0.0; retained immutable tooling commit + GIT_SUBMODULES "" + # Populate the tooling without configuring its own test project. + SOURCE_SUBDIR cmake) + FetchContent_MakeAvailable(sccamerafix_format_validation) + set(_format_validation_source "${sccamerafix_format_validation_SOURCE_DIR}") +endif() +foreach(_format_validation_file .clang-format clang-format-validate.py cmake/FormatValidation.cmake) + if(NOT EXISTS "${_format_validation_source}/${_format_validation_file}") + message(FATAL_ERROR "FORMAT_VALIDATION_SOURCE_PATH is missing ${_format_validation_file}: ${_format_validation_source}") + endif() +endforeach() +include("${_format_validation_source}/cmake/FormatValidation.cmake") +format_validation_add_component(SCCameraFix "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/src/exportfuncs.cpp b/src/exportfuncs.cpp index d6a330e..f66a99a 100644 --- a/src/exportfuncs.cpp +++ b/src/exportfuncs.cpp @@ -15,214 +15,214 @@ cl_enginefunc_t gEngfuncs; cvar_t* cl_chasedist = NULL; -vec3_t v_angles; -vec3_t v_cl_angles; -vec3_t v_sim_org; +vec3_t v_angles; +vec3_t v_cl_angles; +vec3_t v_sim_org; -static struct event_api_s s_ProxyEventAPI = { 0 }; +static struct event_api_s s_ProxyEventAPI = {0}; -static bool g_bIsCallingCAM_Think = false; +static bool g_bIsCallingCAM_Think = false; static bool g_bIsCallingCAM_Think_Post = false; int EngineGetMaxPhysEnts() { - if (g_iEngineType == ENGINE_SVENGINE && g_dwEngineBuildnum >= 10152) - return MAX_PHYSENTS_10152; + if (g_iEngineType == ENGINE_SVENGINE && g_dwEngineBuildnum >= 10152) + return MAX_PHYSENTS_10152; - return MAX_PHYSENTS; + return MAX_PHYSENTS; } void EV_PlayerTrace_Proxy(float* start, float* end, int traceFlags, int ignore_pe, struct pmtrace_s* tr) { - if (g_bIsCallingCAM_Think && traceFlags == (PM_STUDIO_BOX | PM_STUDIO_IGNORE)) - { - gEngfuncs.pEventAPI->EV_SetUpPlayerPrediction(1, 1); - gEngfuncs.pEventAPI->EV_PushPMStates(); + if (g_bIsCallingCAM_Think && traceFlags == (PM_STUDIO_BOX | PM_STUDIO_IGNORE)) + { + gEngfuncs.pEventAPI->EV_SetUpPlayerPrediction(1, 1); + gEngfuncs.pEventAPI->EV_PushPMStates(); - auto spectating_player = gEngfuncs.GetLocalPlayer(); + auto spectating_player = gEngfuncs.GetLocalPlayer(); - if (g_iUser1 && g_iUser2 && (*g_iUser1)) - { - spectating_player = gEngfuncs.GetEntityByIndex((*g_iUser2)); - } + if (g_iUser1 && g_iUser2 && (*g_iUser1)) + { + spectating_player = gEngfuncs.GetEntityByIndex((*g_iUser2)); + } - if (spectating_player->player) - { - gEngfuncs.pEventAPI->EV_SetSolidPlayers(spectating_player->index - 1); - } - else - { - gEngfuncs.pEventAPI->EV_SetSolidPlayers(-1); - } + if (spectating_player->player) + { + gEngfuncs.pEventAPI->EV_SetSolidPlayers(spectating_player->index - 1); + } + else + { + gEngfuncs.pEventAPI->EV_SetSolidPlayers(-1); + } - ignore_pe = -1; + ignore_pe = -1; - for (int i = 0; i < EngineGetMaxPhysEnts(); ++i) - { - auto PhysEnt = gEngfuncs.pEventAPI->EV_GetPhysent(i); + for (int i = 0; i < EngineGetMaxPhysEnts(); ++i) + { + auto PhysEnt = gEngfuncs.pEventAPI->EV_GetPhysent(i); - if (!PhysEnt) - break; + if (!PhysEnt) + break; - if (PhysEnt->info == spectating_player->index) - { - ignore_pe = i; - } - } + if (PhysEnt->info == spectating_player->index) + { + ignore_pe = i; + } + } - gEngfuncs.pEventAPI->EV_SetTraceHull(2); + gEngfuncs.pEventAPI->EV_SetTraceHull(2); - gEngfuncs.pEventAPI->EV_PlayerTrace(start, end, PM_STUDIO_BOX | PM_STUDIO_IGNORE, ignore_pe, tr); + gEngfuncs.pEventAPI->EV_PlayerTrace(start, end, PM_STUDIO_BOX | PM_STUDIO_IGNORE, ignore_pe, tr); - gEngfuncs.pEventAPI->EV_PopPMStates(); + gEngfuncs.pEventAPI->EV_PopPMStates(); - g_bIsCallingCAM_Think_Post = true; + g_bIsCallingCAM_Think_Post = true; - return; - } + return; + } - return gEngfuncs.pEventAPI->EV_PlayerTrace(start, end, traceFlags, ignore_pe, tr); + return gEngfuncs.pEventAPI->EV_PlayerTrace(start, end, traceFlags, ignore_pe, tr); } void EV_SetUpPlayerPrediction_Proxy(int dopred, int bIncludeLocalClient) { - if (g_bIsCallingCAM_Think && g_bIsCallingCAM_Think_Post && dopred == 1 && bIncludeLocalClient == 1) - { - return; - } + if (g_bIsCallingCAM_Think && g_bIsCallingCAM_Think_Post && dopred == 1 && bIncludeLocalClient == 1) + { + return; + } - gEngfuncs.pEventAPI->EV_SetUpPlayerPrediction(dopred, bIncludeLocalClient); + gEngfuncs.pEventAPI->EV_SetUpPlayerPrediction(dopred, bIncludeLocalClient); } void EV_SetSolidPlayers_Proxy(int playernum) { - if (g_bIsCallingCAM_Think && g_bIsCallingCAM_Think_Post && playernum == -1) - { - return; - } + if (g_bIsCallingCAM_Think && g_bIsCallingCAM_Think_Post && playernum == -1) + { + return; + } - gEngfuncs.pEventAPI->EV_SetSolidPlayers(playernum); + gEngfuncs.pEventAPI->EV_SetSolidPlayers(playernum); } void CAM_Think(void) { - g_bIsCallingCAM_Think = true; - g_bIsCallingCAM_Think_Post = false; + g_bIsCallingCAM_Think = true; + g_bIsCallingCAM_Think_Post = false; - gExportfuncs.CAM_Think(); + gExportfuncs.CAM_Think(); - g_bIsCallingCAM_Think = false; - g_bIsCallingCAM_Think_Post = false; + g_bIsCallingCAM_Think = false; + g_bIsCallingCAM_Think_Post = false; } -const vec3_t VEC_VIEW = { 0, 0, 28 }; +const vec3_t VEC_VIEW = {0, 0, 28}; int PM_GetPhysEntInfo(int i) { - auto physent = gEngfuncs.pEventAPI->EV_GetPhysent(i); - if (physent) - return physent->info; - return -1; + auto physent = gEngfuncs.pEventAPI->EV_GetPhysent(i); + if (physent) + return physent->info; + return -1; } // Get the origin of the Observer based around the target's position and angles void V_GetChaseOrigin(const float* angles, const float* origin, float distance, vec3_t& returnvec) { - vec3_t vecEnd; - vec3_t forward, right, up; - vec3_t vecStart; - pmtrace_t* trace; - int maxLoops = 8; + vec3_t vecEnd; + vec3_t forward, right, up; + vec3_t vecStart; + pmtrace_t* trace; + int maxLoops = 8; - int ignorePhysEntIndex = -1; // first, ignore no entity + int ignorePhysEntIndex = -1; // first, ignore no entity - cl_entity_t* ent = nullptr; + cl_entity_t* ent = nullptr; - // Trace back from the target using the player's view angles - AngleVectors(angles, forward, right, up); + // Trace back from the target using the player's view angles + AngleVectors(angles, forward, right, up); - forward[0] = -forward[0]; - forward[1] = -forward[1]; - forward[2] = -forward[2]; + forward[0] = -forward[0]; + forward[1] = -forward[1]; + forward[2] = -forward[2]; - vecStart[0] = origin[0]; - vecStart[1] = origin[1]; - vecStart[2] = origin[2]; + vecStart[0] = origin[0]; + vecStart[1] = origin[1]; + vecStart[2] = origin[2]; - vecEnd[0] = vecStart[0] + (distance * forward[0]); - vecEnd[1] = vecStart[1] + (distance * forward[1]); - vecEnd[2] = vecStart[2] + (distance * forward[2]); + vecEnd[0] = vecStart[0] + (distance * forward[0]); + vecEnd[1] = vecStart[1] + (distance * forward[1]); + vecEnd[2] = vecStart[2] + (distance * forward[2]); - while (maxLoops > 0) - { - trace = gEngfuncs.PM_TraceLine(vecStart, vecEnd, PM_TRACELINE_PHYSENTSONLY, 2, ignorePhysEntIndex); + while (maxLoops > 0) + { + trace = gEngfuncs.PM_TraceLine(vecStart, vecEnd, PM_TRACELINE_PHYSENTSONLY, 2, ignorePhysEntIndex); - // WARNING! trace->ent is is the number in physent list not the normal entity number + // WARNING! trace->ent is is the number in physent list not the normal entity number - if (trace->ent <= 0) - break; // we hit the world or nothing, stop trace + if (trace->ent <= 0) + break; // we hit the world or nothing, stop trace - ent = gEngfuncs.GetEntityByIndex(PM_GetPhysEntInfo(trace->ent)); + ent = gEngfuncs.GetEntityByIndex(PM_GetPhysEntInfo(trace->ent)); - if (ent == nullptr) - break; + if (ent == nullptr) + break; - // hit non-player solid BSP , stop here - if (ent->curstate.solid == SOLID_BSP && 0 == ent->player) - break; + // hit non-player solid BSP , stop here + if (ent->curstate.solid == SOLID_BSP && 0 == ent->player) + break; - // if close enought to end pos, stop, otherwise continue trace - if (VectorDistance(vecEnd, trace->endpos) < 1.0f) - { - break; - } - else - { - ignorePhysEntIndex = trace->ent; // ignore last hit entity - vecStart[0] = trace->endpos[0]; - vecStart[1] = trace->endpos[1]; - vecStart[2] = trace->endpos[2]; - } + // if close enought to end pos, stop, otherwise continue trace + if (VectorDistance(vecEnd, trace->endpos) < 1.0f) + { + break; + } + else + { + ignorePhysEntIndex = trace->ent; // ignore last hit entity + vecStart[0] = trace->endpos[0]; + vecStart[1] = trace->endpos[1]; + vecStart[2] = trace->endpos[2]; + } - maxLoops--; - } + maxLoops--; + } - returnvec[0] = trace->endpos[0] + (4 * trace->plane.normal[0]); - returnvec[1] = trace->endpos[1] + (4 * trace->plane.normal[1]); - returnvec[2] = trace->endpos[2] + (4 * trace->plane.normal[2]); + returnvec[0] = trace->endpos[0] + (4 * trace->plane.normal[0]); + returnvec[1] = trace->endpos[1] + (4 * trace->plane.normal[1]); + returnvec[2] = trace->endpos[2] + (4 * trace->plane.normal[2]); } void V_GetChasePos(int target, vec3_t* cl_angles, vec3_t& origin, vec3_t& angles) { - cl_entity_t* ent = nullptr; - - if (0 != target) - { - ent = gEngfuncs.GetEntityByIndex(target); - } - - if (!ent) - { - // just copy a save in-map position - VectorCopy(gEngfuncs.GetLocalPlayer()->angles, angles); - VectorCopy(gEngfuncs.GetLocalPlayer()->origin, origin); - return; - } - - if (cl_angles == nullptr) // no mouse angles given, use entity angles ( locked mode ) - { - VectorCopy(ent->angles, angles); - angles[0] *= -1; - } - else - { - VectorCopy((*cl_angles), angles); - } - - VectorCopy(ent->origin, origin); - VectorAdd(origin, VEC_VIEW, origin); - - V_GetChaseOrigin(angles, origin, cl_chasedist ? cl_chasedist->value : 128, origin); + cl_entity_t* ent = nullptr; + + if (0 != target) + { + ent = gEngfuncs.GetEntityByIndex(target); + } + + if (!ent) + { + // just copy a save in-map position + VectorCopy(gEngfuncs.GetLocalPlayer()->angles, angles); + VectorCopy(gEngfuncs.GetLocalPlayer()->origin, origin); + return; + } + + if (cl_angles == nullptr) // no mouse angles given, use entity angles ( locked mode ) + { + VectorCopy(ent->angles, angles); + angles[0] *= -1; + } + else + { + VectorCopy((*cl_angles), angles); + } + + VectorCopy(ent->origin, origin); + VectorAdd(origin, VEC_VIEW, origin); + + V_GetChaseOrigin(angles, origin, cl_chasedist ? cl_chasedist->value : 128, origin); } /* @@ -232,77 +232,77 @@ V_CalcSpectatorRefdef */ void V_CalcSpectatorRefdef(ref_params_t* pparams) { - pparams->onlyClientDraw = 0; - - // refresh position - VectorCopy(pparams->simorg, v_sim_org); - - // get old values - VectorCopy(pparams->cl_viewangles, v_cl_angles); - VectorCopy(pparams->viewangles, v_angles); - VectorCopy(pparams->vieworg, (*v_origin)); - - switch ((*g_iUser1)) - { - case OBS_SVEN_CHASE_FREE: - { - V_GetChasePos((*g_iUser2), &v_cl_angles, (*v_origin), v_angles); - break; - } - case OBS_SVEN_ROAMING: - { - VectorCopy(v_cl_angles, v_angles); - VectorCopy(v_sim_org, (*v_origin)); - break; - } - case OBS_SVEN_CHASE_LOCKED: - { - V_GetChasePos((*g_iUser2), nullptr, (*v_origin), v_angles); - break; - } - } - - // Write back new values into pparams - VectorCopy(v_cl_angles, pparams->cl_viewangles); - VectorCopy(v_angles, pparams->viewangles); - VectorCopy((*v_origin), pparams->vieworg); - - //For SoundEngine - VectorCopy(pparams->viewangles, (*g_vVecViewangles)); + pparams->onlyClientDraw = 0; + + // refresh position + VectorCopy(pparams->simorg, v_sim_org); + + // get old values + VectorCopy(pparams->cl_viewangles, v_cl_angles); + VectorCopy(pparams->viewangles, v_angles); + VectorCopy(pparams->vieworg, (*v_origin)); + + switch ((*g_iUser1)) + { + case OBS_SVEN_CHASE_FREE: + { + V_GetChasePos((*g_iUser2), &v_cl_angles, (*v_origin), v_angles); + break; + } + case OBS_SVEN_ROAMING: + { + VectorCopy(v_cl_angles, v_angles); + VectorCopy(v_sim_org, (*v_origin)); + break; + } + case OBS_SVEN_CHASE_LOCKED: + { + V_GetChasePos((*g_iUser2), nullptr, (*v_origin), v_angles); + break; + } + } + + // Write back new values into pparams + VectorCopy(v_cl_angles, pparams->cl_viewangles); + VectorCopy(v_angles, pparams->viewangles); + VectorCopy((*v_origin), pparams->vieworg); + + //For SoundEngine + VectorCopy(pparams->viewangles, (*g_vVecViewangles)); } void V_CalcNormalRefdef(ref_params_t* pparams) { - if (pparams->spectator || (*g_iUser1)) - { - V_CalcSpectatorRefdef(pparams); - return; - } + if (pparams->spectator || (*g_iUser1)) + { + V_CalcSpectatorRefdef(pparams); + return; + } - gPrivateFuncs.V_CalcNormalRefdef(pparams); + gPrivateFuncs.V_CalcNormalRefdef(pparams); } void HUD_Init(void) { - gExportfuncs.HUD_Init(); + gExportfuncs.HUD_Init(); - cl_chasedist = gEngfuncs.pfnGetCvarPointer("cl_chasedist"); - if (!cl_chasedist) - cl_chasedist = gEngfuncs.pfnRegisterVariable("cl_chasedist", "128", FCVAR_CLIENTDLL | FCVAR_ARCHIVE); + cl_chasedist = gEngfuncs.pfnGetCvarPointer("cl_chasedist"); + if (!cl_chasedist) + cl_chasedist = gEngfuncs.pfnRegisterVariable("cl_chasedist", "128", FCVAR_CLIENTDLL | FCVAR_ARCHIVE); - memcpy(&s_ProxyEventAPI, gEngfuncs.pEventAPI, sizeof(s_ProxyEventAPI)); + memcpy(&s_ProxyEventAPI, gEngfuncs.pEventAPI, sizeof(s_ProxyEventAPI)); - s_ProxyEventAPI.EV_PlayerTrace = EV_PlayerTrace_Proxy; - s_ProxyEventAPI.EV_SetUpPlayerPrediction = EV_SetUpPlayerPrediction_Proxy; - s_ProxyEventAPI.EV_SetSolidPlayers = EV_SetSolidPlayers_Proxy; + s_ProxyEventAPI.EV_PlayerTrace = EV_PlayerTrace_Proxy; + s_ProxyEventAPI.EV_SetUpPlayerPrediction = EV_SetUpPlayerPrediction_Proxy; + s_ProxyEventAPI.EV_SetSolidPlayers = EV_SetSolidPlayers_Proxy; - (*g_pClientDLLEventAPI) = &s_ProxyEventAPI; + (*g_pClientDLLEventAPI) = &s_ProxyEventAPI; } void HUD_Shutdown(void) { - Client_UninstallHooks(); + Client_UninstallHooks(); - gExportfuncs.HUD_Shutdown(); + gExportfuncs.HUD_Shutdown(); } \ No newline at end of file diff --git a/src/exportfuncs.h b/src/exportfuncs.h index dabab15..211dbfb 100644 --- a/src/exportfuncs.h +++ b/src/exportfuncs.h @@ -7,7 +7,7 @@ void CAM_Think(void); void HUD_Init(void); void HUD_Shutdown(void); -#define OBS_SVEN_NONE 0 -#define OBS_SVEN_CHASE_FREE 1 -#define OBS_SVEN_ROAMING 2 -#define OBS_SVEN_CHASE_LOCKED 3 \ No newline at end of file +#define OBS_SVEN_NONE 0 +#define OBS_SVEN_CHASE_FREE 1 +#define OBS_SVEN_ROAMING 2 +#define OBS_SVEN_CHASE_LOCKED 3 \ No newline at end of file diff --git a/src/mathlib2.cpp b/src/mathlib2.cpp index 008fb36..1dbb76b 100644 --- a/src/mathlib2.cpp +++ b/src/mathlib2.cpp @@ -9,7 +9,7 @@ #include #include -vec3_t vec3_origin = { 0, 0, 0 }; +vec3_t vec3_origin = {0, 0, 0}; int nanmask = 255 << 23; @@ -17,881 +17,942 @@ int nanmask = 255 << 23; void ProjectPointOnPlane(vec3_t dst, const vec3_t p, const vec3_t normal) { - float d; - vec3_t n; - float inv_denom; + float d; + vec3_t n; + float inv_denom; - inv_denom = 1.0F / DotProduct(normal, normal); + inv_denom = 1.0F / DotProduct(normal, normal); - d = DotProduct(normal, p) * inv_denom; + d = DotProduct(normal, p) * inv_denom; - n[0] = normal[0] * inv_denom; - n[1] = normal[1] * inv_denom; - n[2] = normal[2] * inv_denom; + n[0] = normal[0] * inv_denom; + n[1] = normal[1] * inv_denom; + n[2] = normal[2] * inv_denom; - dst[0] = p[0] - d * n[0]; - dst[1] = p[1] - d * n[1]; - dst[2] = p[2] - d * n[2]; + dst[0] = p[0] - d * n[0]; + dst[1] = p[1] - d * n[1]; + dst[2] = p[2] - d * n[2]; } void PerpendicularVector(vec3_t dst, const vec3_t src) { - int pos; - int i; - float minelem = 1.0F; - vec3_t tempvec; + int pos; + int i; + float minelem = 1.0F; + vec3_t tempvec; - for (pos = 0, i = 0; i < 3; i++) - { - if (fabs(src[i]) < minelem) - { - pos = i; - minelem = fabs(src[i]); - } - } + for (pos = 0, i = 0; i < 3; i++) + { + if (fabs(src[i]) < minelem) + { + pos = i; + minelem = fabs(src[i]); + } + } - tempvec[0] = tempvec[1] = tempvec[2] = 0.0F; - tempvec[pos] = 1.0F; + tempvec[0] = tempvec[1] = tempvec[2] = 0.0F; + tempvec[pos] = 1.0F; - ProjectPointOnPlane(dst, tempvec, src); + ProjectPointOnPlane(dst, tempvec, src); - VectorNormalize(dst); + VectorNormalize(dst); } #ifdef _WIN32 -#pragma optimize("", off) +# pragma optimize("", off) #endif void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees) { - float m[3][3]; - float im[3][3]; - float zrot[3][3]; - float tmpmat[3][3]; - float rot[3][3]; - int i; - vec3_t vr, vup, vf; + float m[3][3]; + float im[3][3]; + float zrot[3][3]; + float tmpmat[3][3]; + float rot[3][3]; + int i; + vec3_t vr, vup, vf; - vf[0] = dir[0]; - vf[1] = dir[1]; - vf[2] = dir[2]; + vf[0] = dir[0]; + vf[1] = dir[1]; + vf[2] = dir[2]; - PerpendicularVector(vr, dir); - CrossProduct(vr, vf, vup); + PerpendicularVector(vr, dir); + CrossProduct(vr, vf, vup); - m[0][0] = vr[0]; - m[1][0] = vr[1]; - m[2][0] = vr[2]; + m[0][0] = vr[0]; + m[1][0] = vr[1]; + m[2][0] = vr[2]; - m[0][1] = vup[0]; - m[1][1] = vup[1]; - m[2][1] = vup[2]; + m[0][1] = vup[0]; + m[1][1] = vup[1]; + m[2][1] = vup[2]; - m[0][2] = vf[0]; - m[1][2] = vf[1]; - m[2][2] = vf[2]; + m[0][2] = vf[0]; + m[1][2] = vf[1]; + m[2][2] = vf[2]; - memcpy(im, m, sizeof(im)); + memcpy(im, m, sizeof(im)); - im[0][1] = m[1][0]; - im[0][2] = m[2][0]; - im[1][0] = m[0][1]; - im[1][2] = m[2][1]; - im[2][0] = m[0][2]; - im[2][1] = m[1][2]; + im[0][1] = m[1][0]; + im[0][2] = m[2][0]; + im[1][0] = m[0][1]; + im[1][2] = m[2][1]; + im[2][0] = m[0][2]; + im[2][1] = m[1][2]; - memset(zrot, 0, sizeof(zrot)); - zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F; + memset(zrot, 0, sizeof(zrot)); + zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F; - zrot[0][0] = cos(DEG2RAD(degrees)); - zrot[0][1] = sin(DEG2RAD(degrees)); - zrot[1][0] = -sin(DEG2RAD(degrees)); - zrot[1][1] = cos(DEG2RAD(degrees)); + zrot[0][0] = cos(DEG2RAD(degrees)); + zrot[0][1] = sin(DEG2RAD(degrees)); + zrot[1][0] = -sin(DEG2RAD(degrees)); + zrot[1][1] = cos(DEG2RAD(degrees)); - R_ConcatRotations(m, zrot, tmpmat); - R_ConcatRotations(tmpmat, im, rot); + R_ConcatRotations(m, zrot, tmpmat); + R_ConcatRotations(tmpmat, im, rot); - for (i = 0; i < 3; i++) - dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2]; + for (i = 0; i < 3; i++) + dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2]; } #ifdef _WIN32 -#pragma optimize("", on) +# pragma optimize("", on) #endif -int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, mplane_t *p) -{ - float dist1, dist2; - int sides; - - switch (p->signbits) - { - case 0: - { - dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; - dist2 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; - break; - } - - case 1: - { - dist1 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; - dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; - break; - } - - case 2: - { - dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; - dist2 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; - break; - } - - case 3: - { - dist1 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; - dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; - break; - } - - case 4: - { - dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; - dist2 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; - break; - } - - case 5: - { - dist1 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; - dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; - break; - } - - case 6: - { - dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; - dist2 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; - break; - } - - case 7: - { - dist1 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; - dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; - break; - } - - default: - { - dist1 = dist2 = 0; - break; - } - } - - sides = 0; - - if (dist1 >= p->dist) - sides = 1; - - if (dist2 < p->dist) - sides |= 2; - - return sides; +int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, mplane_t* p) +{ + float dist1, dist2; + int sides; + + switch (p->signbits) + { + case 0: + { + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + break; + } + + case 1: + { + dist1 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + break; + } + + case 2: + { + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + break; + } + + case 3: + { + dist1 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + break; + } + + case 4: + { + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + break; + } + + case 5: + { + dist1 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + break; + } + + case 6: + { + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + break; + } + + case 7: + { + dist1 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + break; + } + + default: + { + dist1 = dist2 = 0; + break; + } + } + + sides = 0; + + if (dist1 >= p->dist) + sides = 1; + + if (dist2 < p->dist) + sides |= 2; + + return sides; } void AngleVectors(const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) { - float angle; - float sr, sp, sy, cr, cp, cy; - - angle = angles[1] * (M_PI * 2 / 360); - sy = sin(angle); - cy = cos(angle); - angle = angles[0] * (M_PI * 2 / 360); - sp = sin(angle); - cp = cos(angle); - angle = angles[2] * (M_PI * 2 / 360); - sr = sin(angle); - cr = cos(angle); - - forward[0] = cp * cy; - forward[1] = cp * sy; - forward[2] = -sp; - right[0] = (-1 * sr * sp * cy + -1 * cr * -sy); - right[1] = (-1 * sr * sp * sy + -1 * cr * cy); - right[2] = -1 * sr * cp; - up[0] = (cr * sp * cy + -sr * -sy); - up[1] = (cr * sp * sy + -sr * cy); - up[2] = cr * cp; -} - -void AngleVectorsTranspose(const vec3_t angles, vec3_t *forward, vec3_t *right, vec3_t *up) -{ - float angle; - float sr, sp, sy, cr, cp, cy; - - angle = angles[1] * (M_PI * 2 / 360); - sy = sin(angle); - cy = cos(angle); - angle = angles[0] * (M_PI * 2 / 360); - sp = sin(angle); - cp = cos(angle); - angle = angles[2] * (M_PI * 2 / 360); - sr = sin(angle); - cr = cos(angle); - - if (forward) - { - (*forward)[0] = cp * cy; - (*forward)[1] = cp * sy; - (*forward)[2] = -sp; - } - - if (right) - { - (*right)[0] = (-1 * sr * sp * cy + -1 * cr * -sy); - (*right)[1] = (-1 * sr * sp * sy + -1 * cr * cy); - (*right)[2] = -1 * sr * cp; - } - - if (up) - { - (*up)[0] = (cr * sp * cy + -sr * -sy); - (*up)[1] = (cr * sp * sy + -sr * cy); - (*up)[2] = cr * cp; - } - + float angle; + float sr, sp, sy, cr, cp, cy; + + angle = angles[1] * (M_PI * 2 / 360); + sy = sin(angle); + cy = cos(angle); + angle = angles[0] * (M_PI * 2 / 360); + sp = sin(angle); + cp = cos(angle); + angle = angles[2] * (M_PI * 2 / 360); + sr = sin(angle); + cr = cos(angle); + + forward[0] = cp * cy; + forward[1] = cp * sy; + forward[2] = -sp; + right[0] = (-1 * sr * sp * cy + -1 * cr * -sy); + right[1] = (-1 * sr * sp * sy + -1 * cr * cy); + right[2] = -1 * sr * cp; + up[0] = (cr * sp * cy + -sr * -sy); + up[1] = (cr * sp * sy + -sr * cy); + up[2] = cr * cp; +} + +void AngleVectorsTranspose(const vec3_t angles, vec3_t* forward, vec3_t* right, vec3_t* up) +{ + float angle; + float sr, sp, sy, cr, cp, cy; + + angle = angles[1] * (M_PI * 2 / 360); + sy = sin(angle); + cy = cos(angle); + angle = angles[0] * (M_PI * 2 / 360); + sp = sin(angle); + cp = cos(angle); + angle = angles[2] * (M_PI * 2 / 360); + sr = sin(angle); + cr = cos(angle); + + if (forward) + { + (*forward)[0] = cp * cy; + (*forward)[1] = cp * sy; + (*forward)[2] = -sp; + } + + if (right) + { + (*right)[0] = (-1 * sr * sp * cy + -1 * cr * -sy); + (*right)[1] = (-1 * sr * sp * sy + -1 * cr * cy); + (*right)[2] = -1 * sr * cp; + } + + if (up) + { + (*up)[0] = (cr * sp * cy + -sr * -sy); + (*up)[1] = (cr * sp * sy + -sr * cy); + (*up)[2] = cr * cp; + } } void VectorAngles(const vec3_t forward, vec3_t angles) { - float tmp, yaw, pitch; + float tmp, yaw, pitch; - if (forward[1] == 0 && forward[0] == 0) - { - yaw = 0; + if (forward[1] == 0 && forward[0] == 0) + { + yaw = 0; - if (forward[2] > 0) - pitch = 90; - else - pitch = 270; - } - else - { - yaw = (atan2(forward[1], forward[0]) * 180 / M_PI); + if (forward[2] > 0) + pitch = 90; + else + pitch = 270; + } + else + { + yaw = (atan2(forward[1], forward[0]) * 180 / M_PI); - if (yaw < 0) - yaw += 360; + if (yaw < 0) + yaw += 360; - tmp = sqrt(forward[0] * forward[0] + forward[1] * forward[1]); - pitch = (atan2(forward[2], tmp) * 180 / M_PI); + tmp = sqrt(forward[0] * forward[0] + forward[1] * forward[1]); + pitch = (atan2(forward[2], tmp) * 180 / M_PI); - if (pitch < 0) - pitch += 360; - } + if (pitch < 0) + pitch += 360; + } - angles[0] = pitch; - angles[1] = yaw; - angles[2] = 0; + angles[0] = pitch; + angles[1] = yaw; + angles[2] = 0; } double VectorLength(vec3_t v) { - int i; - double length; + int i; + double length; - length = 0; + length = 0; - for (i = 0; i < 3; i++) - length += v[i] * v[i]; + for (i = 0; i < 3; i++) + length += v[i] * v[i]; - length = sqrt(length); - return length; + length = sqrt(length); + return length; } double VectorDistance(const vec3_t v1, const vec3_t v2) { - vec3_t v3; - VectorSubtract(v1, v2, v3); - return VectorLength(v3); + vec3_t v3; + VectorSubtract(v1, v2, v3); + return VectorLength(v3); } #ifndef FLT_EPSILON -#define FLT_EPSILON (1.192093e-007) +# define FLT_EPSILON (1.192093e-007) #endif int VectorCompare(const vec3_t v1, const vec3_t v2) { - int i; + int i; - for (i = 0; i < 3; i++) - { - if (fabs(v1[i] - v2[i]) > FLT_EPSILON) - return 0; - } + for (i = 0; i < 3; i++) + { + if (fabs(v1[i] - v2[i]) > FLT_EPSILON) + return 0; + } - return 1; + return 1; } vec_t Q_rint(vec_t in) { - return floor(in + 0.5); + return floor(in + 0.5); } void VectorMA(const vec3_t va, float scale, const vec3_t vb, vec3_t vc) { - vc[0] = va[0] + scale * vb[0]; - vc[1] = va[1] + scale * vb[1]; - vc[2] = va[2] + scale * vb[2]; + vc[0] = va[0] + scale * vb[0]; + vc[1] = va[1] + scale * vb[1]; + vc[2] = va[2] + scale * vb[2]; } void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross) { - cross[0] = v1[1] * v2[2] - v1[2] * v2[1]; - cross[1] = v1[2] * v2[0] - v1[0] * v2[2]; - cross[2] = v1[0] * v2[1] - v1[1] * v2[0]; + cross[0] = v1[1] * v2[2] - v1[2] * v2[1]; + cross[1] = v1[2] * v2[0] - v1[0] * v2[2]; + cross[2] = v1[0] * v2[1] - v1[1] * v2[0]; } vec_t _DotProduct(vec3_t v1, vec3_t v2) { - return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; + return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; } void _VectorSubtract(vec3_t va, vec3_t vb, vec3_t out) { - out[0] = va[0] - vb[0]; - out[1] = va[1] - vb[1]; - out[2] = va[2] - vb[2]; + out[0] = va[0] - vb[0]; + out[1] = va[1] - vb[1]; + out[2] = va[2] - vb[2]; } void _VectorAdd(vec3_t va, vec3_t vb, vec3_t out) { - out[0] = va[0] + vb[0]; - out[1] = va[1] + vb[1]; - out[2] = va[2] + vb[2]; + out[0] = va[0] + vb[0]; + out[1] = va[1] + vb[1]; + out[2] = va[2] + vb[2]; } void _VectorCopy(vec3_t in, vec3_t out) { - out[0] = in[0]; - out[1] = in[1]; - out[2] = in[2]; + out[0] = in[0]; + out[1] = in[1]; + out[2] = in[2]; } void _VectorScale(vec3_t v, vec_t scale, vec3_t out) { - out[0] = v[0] * scale; - out[1] = v[1] * scale; - out[2] = v[2] * scale; + out[0] = v[0] * scale; + out[1] = v[1] * scale; + out[2] = v[2] * scale; } vec_t Length(const vec3_t v) { - int i; - float length; + int i; + float length; - length = 0; + length = 0; - for (i = 0; i< 3; i++) - length += v[i] * v[i]; + for (i = 0; i < 3; i++) + length += v[i] * v[i]; - length = sqrt(length); - return length; + length = sqrt(length); + return length; } vec_t VectorNormalize(vec3_t v) { - int i; - double length; + int i; + double length; - if (fabs(v[1] - 0.000215956) < 0.0001) - i = 1; + if (fabs(v[1] - 0.000215956) < 0.0001) + i = 1; - length = 0; + length = 0; - for (i = 0; i < 3; i++) - length += v[i] * v[i]; + for (i = 0; i < 3; i++) + length += v[i] * v[i]; - length = sqrt(length); + length = sqrt(length); - if (length == 0) - return 0; + if (length == 0) + return 0; - for (i = 0; i < 3; i++) - v[i] /= length; + for (i = 0; i < 3; i++) + v[i] /= length; - return length; + return length; } void VectorInverse(vec3_t v) { - v[0] = -v[0]; - v[1] = -v[1]; - v[2] = -v[2]; + v[0] = -v[0]; + v[1] = -v[1]; + v[2] = -v[2]; } void VectorScale(const vec3_t in, vec_t scale, vec3_t out) { - out[0] = in[0] * scale; - out[1] = in[1] * scale; - out[2] = in[2] * scale; + out[0] = in[0] * scale; + out[1] = in[1] * scale; + out[2] = in[2] * scale; } void ClearBounds(vec3_t mins, vec3_t maxs) { - mins[0] = mins[1] = mins[2] = 99999; - maxs[0] = maxs[1] = maxs[2] = -99999; + mins[0] = mins[1] = mins[2] = 99999; + maxs[0] = maxs[1] = maxs[2] = -99999; } void MatrixCopy(float in[4][3], float out[4][3]) { - memcpy(out, in, sizeof(float) * 3 * 4); + memcpy(out, in, sizeof(float) * 3 * 4); } void AddPointToBounds(vec3_t v, vec3_t mins, vec3_t maxs) { - int i; - vec_t val; + int i; + vec_t val; - for (i = 0; i < 3; i++) - { - val = v[i]; + for (i = 0; i < 3; i++) + { + val = v[i]; - if (val < mins[i]) - mins[i] = val; + if (val < mins[i]) + mins[i] = val; - if (val > maxs[i]) - maxs[i] = val; - } + if (val > maxs[i]) + maxs[i] = val; + } } void AngleMatrix(const vec3_t angles, float (*matrix)[4]) { - float angle; - float sr, sp, sy, cr, cp, cy; - - angle = angles[2] * (M_PI * 2 / 360); - sy = sin(angle); - cy = cos(angle); - angle = angles[1] * (M_PI * 2 / 360); - sp = sin(angle); - cp = cos(angle); - angle = angles[0] * (M_PI * 2 / 360); - sr = sin(angle); - cr = cos(angle); - - matrix[0][0] = cp * cy; - matrix[1][0] = cp * sy; - matrix[2][0] = -sp; - matrix[0][1] = sr * sp * cy + cr * -sy; - matrix[1][1] = sr * sp * sy + cr * cy; - matrix[2][1] = sr * cp; - matrix[0][2] = (cr * sp * cy + -sr * -sy); - matrix[1][2] = (cr * sp * sy + -sr * cy); - matrix[2][2] = cr * cp; - matrix[0][3] = 0.0; - matrix[1][3] = 0.0; - matrix[2][3] = 0.0; + float angle; + float sr, sp, sy, cr, cp, cy; + + angle = angles[2] * (M_PI * 2 / 360); + sy = sin(angle); + cy = cos(angle); + angle = angles[1] * (M_PI * 2 / 360); + sp = sin(angle); + cp = cos(angle); + angle = angles[0] * (M_PI * 2 / 360); + sr = sin(angle); + cr = cos(angle); + + matrix[0][0] = cp * cy; + matrix[1][0] = cp * sy; + matrix[2][0] = -sp; + matrix[0][1] = sr * sp * cy + cr * -sy; + matrix[1][1] = sr * sp * sy + cr * cy; + matrix[2][1] = sr * cp; + matrix[0][2] = (cr * sp * cy + -sr * -sy); + matrix[1][2] = (cr * sp * sy + -sr * cy); + matrix[2][2] = cr * cp; + matrix[0][3] = 0.0; + matrix[1][3] = 0.0; + matrix[2][3] = 0.0; } void AngleIMatrix(const vec3_t angles, float matrix[3][4]) { - float angle; - float sr, sp, sy, cr, cp, cy; - - angle = angles[2] * (M_PI * 2 / 360); - sy = sin(angle); - cy = cos(angle); - angle = angles[1] * (M_PI * 2 / 360); - sp = sin(angle); - cp = cos(angle); - angle = angles[0] * (M_PI * 2 / 360); - sr = sin(angle); - cr = cos(angle); - - matrix[0][0] = cp * cy; - matrix[0][1] = cp * sy; - matrix[0][2] = -sp; - matrix[1][0] = sr * sp * cy + cr * -sy; - matrix[1][1] = sr * sp * sy + cr * cy; - matrix[1][2] = sr * cp; - matrix[2][0] = (cr * sp * cy + -sr * -sy); - matrix[2][1] = (cr * sp * sy + -sr * cy); - matrix[2][2] = cr * cp; - matrix[0][3] = 0.0; - matrix[1][3] = 0.0; - matrix[2][3] = 0.0; + float angle; + float sr, sp, sy, cr, cp, cy; + + angle = angles[2] * (M_PI * 2 / 360); + sy = sin(angle); + cy = cos(angle); + angle = angles[1] * (M_PI * 2 / 360); + sp = sin(angle); + cp = cos(angle); + angle = angles[0] * (M_PI * 2 / 360); + sr = sin(angle); + cr = cos(angle); + + matrix[0][0] = cp * cy; + matrix[0][1] = cp * sy; + matrix[0][2] = -sp; + matrix[1][0] = sr * sp * cy + cr * -sy; + matrix[1][1] = sr * sp * sy + cr * cy; + matrix[1][2] = sr * cp; + matrix[2][0] = (cr * sp * cy + -sr * -sy); + matrix[2][1] = (cr * sp * sy + -sr * cy); + matrix[2][2] = cr * cp; + matrix[0][3] = 0.0; + matrix[1][3] = 0.0; + matrix[2][3] = 0.0; } void R_ConcatRotations(float in1[3][3], float in2[3][3], float out[3][3]) { - out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0]; - out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1]; - out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2]; - out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0]; - out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1]; - out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2]; - out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0]; - out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1]; - out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2]; + out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0]; + out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1]; + out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2]; + out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0]; + out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1]; + out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2]; + out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0]; + out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1]; + out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2]; } void R_ConcatTransforms(float in1[3][4], float in2[3][4], float out[3][4]) { - out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0]; - out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1]; - out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2]; - out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3]; - out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0]; - out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1]; - out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2]; - out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3]; - out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0]; - out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1]; - out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2]; - out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3]; + out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0]; + out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1]; + out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2]; + out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3]; + out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0]; + out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1]; + out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2]; + out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3]; + out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0]; + out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1]; + out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2]; + out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3]; } void VectorRotate(const vec3_t in1, const float in2[3][4], vec3_t out) { - out[0] = DotProduct(in1, in2[0]); - out[1] = DotProduct(in1, in2[1]); - out[2] = DotProduct(in1, in2[2]); + out[0] = DotProduct(in1, in2[0]); + out[1] = DotProduct(in1, in2[1]); + out[2] = DotProduct(in1, in2[2]); } void VectorIRotate(const vec3_t in1, const float in2[3][4], vec3_t out) { - out[0] = in1[0] * in2[0][0] + in1[1] * in2[1][0] + in1[2] * in2[2][0]; - out[1] = in1[0] * in2[0][1] + in1[1] * in2[1][1] + in1[2] * in2[2][1]; - out[2] = in1[0] * in2[0][2] + in1[1] * in2[1][2] + in1[2] * in2[2][2]; + out[0] = in1[0] * in2[0][0] + in1[1] * in2[1][0] + in1[2] * in2[2][0]; + out[1] = in1[0] * in2[0][1] + in1[1] * in2[1][1] + in1[2] * in2[2][1]; + out[2] = in1[0] * in2[0][2] + in1[1] * in2[1][2] + in1[2] * in2[2][2]; } void VectorTransform(const vec3_t in1, float in2[3][4], vec3_t out) { - out[0] = DotProduct(in1, in2[0]) + in2[0][3]; - out[1] = DotProduct(in1, in2[1]) + in2[1][3]; - out[2] = DotProduct(in1, in2[2]) + in2[2][3]; + out[0] = DotProduct(in1, in2[0]) + in2[0][3]; + out[1] = DotProduct(in1, in2[1]) + in2[1][3]; + out[2] = DotProduct(in1, in2[2]) + in2[2][3]; } void AngleQuaternion(const vec3_t angles, vec4_t quaternion) { - float angle; - float sr, sp, sy, cr, cp, cy; + float angle; + float sr, sp, sy, cr, cp, cy; - angle = angles[2] * 0.5; - sy = sin(angle); - cy = cos(angle); - angle = angles[1] * 0.5; - sp = sin(angle); - cp = cos(angle); - angle = angles[0] * 0.5; - sr = sin(angle); - cr = cos(angle); + angle = angles[2] * 0.5; + sy = sin(angle); + cy = cos(angle); + angle = angles[1] * 0.5; + sp = sin(angle); + cp = cos(angle); + angle = angles[0] * 0.5; + sr = sin(angle); + cr = cos(angle); - quaternion[0] = sr * cp * cy - cr * sp * sy; - quaternion[1] = cr * sp * cy + sr * cp * sy; - quaternion[2] = cr * cp * sy - sr * sp * cy; - quaternion[3] = cr * cp * cy + sr * sp * sy; + quaternion[0] = sr * cp * cy - cr * sp * sy; + quaternion[1] = cr * sp * cy + sr * cp * sy; + quaternion[2] = cr * cp * sy - sr * sp * cy; + quaternion[3] = cr * cp * cy + sr * sp * sy; } void QuaternionMatrix(const vec4_t quaternion, float (*matrix)[4]) { - matrix[0][0] = 1.0 - 2.0 * quaternion[1] * quaternion[1] - 2.0 * quaternion[2] * quaternion[2]; - matrix[1][0] = 2.0 * quaternion[0] * quaternion[1] + 2.0 * quaternion[3] * quaternion[2]; - matrix[2][0] = 2.0 * quaternion[0] * quaternion[2] - 2.0 * quaternion[3] * quaternion[1]; + matrix[0][0] = 1.0 - 2.0 * quaternion[1] * quaternion[1] - 2.0 * quaternion[2] * quaternion[2]; + matrix[1][0] = 2.0 * quaternion[0] * quaternion[1] + 2.0 * quaternion[3] * quaternion[2]; + matrix[2][0] = 2.0 * quaternion[0] * quaternion[2] - 2.0 * quaternion[3] * quaternion[1]; - matrix[0][1] = 2.0 * quaternion[0] * quaternion[1] - 2.0 * quaternion[3] * quaternion[2]; - matrix[1][1] = 1.0 - 2.0 * quaternion[0] * quaternion[0] - 2.0 * quaternion[2] * quaternion[2]; - matrix[2][1] = 2.0 * quaternion[1] * quaternion[2] + 2.0 * quaternion[3] * quaternion[0]; + matrix[0][1] = 2.0 * quaternion[0] * quaternion[1] - 2.0 * quaternion[3] * quaternion[2]; + matrix[1][1] = 1.0 - 2.0 * quaternion[0] * quaternion[0] - 2.0 * quaternion[2] * quaternion[2]; + matrix[2][1] = 2.0 * quaternion[1] * quaternion[2] + 2.0 * quaternion[3] * quaternion[0]; - matrix[0][2] = 2.0 * quaternion[0] * quaternion[2] + 2.0 * quaternion[3] * quaternion[1]; - matrix[1][2] = 2.0 * quaternion[1] * quaternion[2] - 2.0 * quaternion[3] * quaternion[0]; - matrix[2][2] = 1.0 - 2.0 * quaternion[0] * quaternion[0] - 2.0 * quaternion[1] * quaternion[1]; + matrix[0][2] = 2.0 * quaternion[0] * quaternion[2] + 2.0 * quaternion[3] * quaternion[1]; + matrix[1][2] = 2.0 * quaternion[1] * quaternion[2] - 2.0 * quaternion[3] * quaternion[0]; + matrix[2][2] = 1.0 - 2.0 * quaternion[0] * quaternion[0] - 2.0 * quaternion[1] * quaternion[1]; } void QuaternionSlerp(const vec4_t p, vec4_t q, float t, vec4_t qt) { - int i; - float omega, cosom, sinom, sclp, sclq; - - float a = 0; - float b = 0; - - for (i = 0; i < 4; i++) - { - a += (p[i] - q[i]) * (p[i] - q[i]); - b += (p[i] + q[i]) * (p[i] + q[i]); - } - - if (a > b) - { - for (i = 0; i < 4; i++) - q[i] = -q[i]; - } - - cosom = p[0] * q[0] + p[1] * q[1] + p[2] * q[2] + p[3] * q[3]; - - if ((1.0 + cosom) > 0.00000001) - { - if ((1.0 - cosom) > 0.00000001) - { - omega = acos(cosom); - sinom = sin(omega); - sclp = sin((1.0 - t)*omega) / sinom; - sclq = sin(t * omega) / sinom; - } - else - { - sclp = 1.0 - t; - sclq = t; - } - - for (i = 0; i < 4; i++) - qt[i] = sclp * p[i] + sclq * q[i]; - } - else - { - qt[0] = -p[1]; - qt[1] = p[0]; - qt[2] = -p[3]; - qt[3] = p[2]; - sclp = sin((1.0 - t) * 0.5 * M_PI); - sclq = sin(t * 0.5 * M_PI); - - for (i = 0; i < 3; i++) - qt[i] = sclp * p[i] + sclq * qt[i]; - } -} - -int InvertMatrix(const float *m, float *out) -{ -#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; } -#define MAT(m,r,c) (m)[(c)*4+(r)] - - float wtmp[4][8]; - float m0, m1, m2, m3, s; - float *r0, *r1, *r2, *r3; - - r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; - - r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1), - r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3), - r0[4] = 1.0f, r0[5] = r0[6] = r0[7] = 0.0f, - - r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1), - r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3), - r1[5] = 1.0f, r1[4] = r1[6] = r1[7] = 0.0f, - - r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1), - r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3), - r2[6] = 1.0f, r2[4] = r2[5] = r2[7] = 0.0f, - - r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1), - r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3), - r3[7] = 1.0f, r3[4] = r3[5] = r3[6] = 0.0f; - - if (fabs(r3[0]) > fabs(r2[0])) SWAP_ROWS(r3, r2); - if (fabs(r2[0]) > fabs(r1[0])) SWAP_ROWS(r2, r1); - if (fabs(r1[0]) > fabs(r0[0])) SWAP_ROWS(r1, r0); - if (0.0f == r0[0]) return false; - - m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0]; - s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s; - s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s; - s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s; - s = r0[4]; - - if (s != 0.0f){ r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; } - s = r0[5]; - if (s != 0.0f) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; } - s = r0[6]; - if (s != 0.0f) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; } - s = r0[7]; - if (s != 0.0f) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; } - - if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2); - if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1); - if (0.0f == r1[1]) return false; - - m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1]; - r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2]; - r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3]; - s = r1[4]; if (0.0f != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; } - s = r1[5]; if (0.0f != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; } - s = r1[6]; if (0.0f != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; } - s = r1[7]; if (0.0f != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; } - - if (fabs(r3[2]) > fabs(r2[2])) SWAP_ROWS(r3, r2); - if (0.0f == r2[2]) return false; - - m3 = r3[2] / r2[2]; - r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4], - r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], - r3[7] -= m3 * r2[7]; - - if (0.0f == r3[3]) return false; - - s = 1.0f / r3[3]; - r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s; - - m2 = r2[3]; - s = 1.0f / r2[2]; - r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2), - r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2); - m1 = r1[3]; - r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1, - r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1; - m0 = r0[3]; - r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0, - r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0; - - m1 = r1[2]; - s = 1.0f / r1[1]; - r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1), - r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1); - m0 = r0[2]; - r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0, - r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0; - - m0 = r0[1]; - s = 1.0f / r0[0]; - r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0), - r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0); - - MAT(out, 0, 0) = r0[4]; - MAT(out, 0, 1) = r0[5], MAT(out,0,2) = r0[6]; - MAT(out, 0, 3) = r0[7], MAT(out,1,0) = r1[4]; - MAT(out, 1, 1) = r1[5], MAT(out,1,2) = r1[6]; - MAT(out, 1, 3) = r1[7], MAT(out,2,0) = r2[4]; - MAT(out, 2, 1) = r2[5], MAT(out,2,2) = r2[6]; - MAT(out, 2, 3) = r2[7], MAT(out,3,0) = r3[4]; - MAT(out, 3, 1) = r3[5], MAT(out,3,2) = r3[6]; - MAT(out, 3, 3) = r3[7]; - - return true; + int i; + float omega, cosom, sinom, sclp, sclq; + + float a = 0; + float b = 0; + + for (i = 0; i < 4; i++) + { + a += (p[i] - q[i]) * (p[i] - q[i]); + b += (p[i] + q[i]) * (p[i] + q[i]); + } + + if (a > b) + { + for (i = 0; i < 4; i++) + q[i] = -q[i]; + } + + cosom = p[0] * q[0] + p[1] * q[1] + p[2] * q[2] + p[3] * q[3]; + + if ((1.0 + cosom) > 0.00000001) + { + if ((1.0 - cosom) > 0.00000001) + { + omega = acos(cosom); + sinom = sin(omega); + sclp = sin((1.0 - t) * omega) / sinom; + sclq = sin(t * omega) / sinom; + } + else + { + sclp = 1.0 - t; + sclq = t; + } + + for (i = 0; i < 4; i++) + qt[i] = sclp * p[i] + sclq * q[i]; + } + else + { + qt[0] = -p[1]; + qt[1] = p[0]; + qt[2] = -p[3]; + qt[3] = p[2]; + sclp = sin((1.0 - t) * 0.5 * M_PI); + sclq = sin(t * 0.5 * M_PI); + + for (i = 0; i < 3; i++) + qt[i] = sclp * p[i] + sclq * qt[i]; + } +} + +int InvertMatrix(const float* m, float* out) +{ +#define SWAP_ROWS(a, b) \ + { \ + float* _tmp = a; \ + (a) = (b); \ + (b) = _tmp; \ + } +#define MAT(m, r, c) (m)[(c) * 4 + (r)] + + float wtmp[4][8]; + float m0, m1, m2, m3, s; + float *r0, *r1, *r2, *r3; + + r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; + + r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1), + r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3), + r0[4] = 1.0f, r0[5] = r0[6] = r0[7] = 0.0f, + + r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1), + r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3), + r1[5] = 1.0f, r1[4] = r1[6] = r1[7] = 0.0f, + + r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1), + r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3), + r2[6] = 1.0f, r2[4] = r2[5] = r2[7] = 0.0f, + + r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1), + r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3), + r3[7] = 1.0f, r3[4] = r3[5] = r3[6] = 0.0f; + + if (fabs(r3[0]) > fabs(r2[0])) SWAP_ROWS(r3, r2); + if (fabs(r2[0]) > fabs(r1[0])) SWAP_ROWS(r2, r1); + if (fabs(r1[0]) > fabs(r0[0])) SWAP_ROWS(r1, r0); + if (0.0f == r0[0]) return false; + + m1 = r1[0] / r0[0]; + m2 = r2[0] / r0[0]; + m3 = r3[0] / r0[0]; + s = r0[1]; + r1[1] -= m1 * s; + r2[1] -= m2 * s; + r3[1] -= m3 * s; + s = r0[2]; + r1[2] -= m1 * s; + r2[2] -= m2 * s; + r3[2] -= m3 * s; + s = r0[3]; + r1[3] -= m1 * s; + r2[3] -= m2 * s; + r3[3] -= m3 * s; + s = r0[4]; + + if (s != 0.0f) + { + r1[4] -= m1 * s; + r2[4] -= m2 * s; + r3[4] -= m3 * s; + } + s = r0[5]; + if (s != 0.0f) + { + r1[5] -= m1 * s; + r2[5] -= m2 * s; + r3[5] -= m3 * s; + } + s = r0[6]; + if (s != 0.0f) + { + r1[6] -= m1 * s; + r2[6] -= m2 * s; + r3[6] -= m3 * s; + } + s = r0[7]; + if (s != 0.0f) + { + r1[7] -= m1 * s; + r2[7] -= m2 * s; + r3[7] -= m3 * s; + } + + if (fabs(r3[1]) > fabs(r2[1])) SWAP_ROWS(r3, r2); + if (fabs(r2[1]) > fabs(r1[1])) SWAP_ROWS(r2, r1); + if (0.0f == r1[1]) return false; + + m2 = r2[1] / r1[1]; + m3 = r3[1] / r1[1]; + r2[2] -= m2 * r1[2]; + r3[2] -= m3 * r1[2]; + r2[3] -= m2 * r1[3]; + r3[3] -= m3 * r1[3]; + s = r1[4]; + if (0.0f != s) + { + r2[4] -= m2 * s; + r3[4] -= m3 * s; + } + s = r1[5]; + if (0.0f != s) + { + r2[5] -= m2 * s; + r3[5] -= m3 * s; + } + s = r1[6]; + if (0.0f != s) + { + r2[6] -= m2 * s; + r3[6] -= m3 * s; + } + s = r1[7]; + if (0.0f != s) + { + r2[7] -= m2 * s; + r3[7] -= m3 * s; + } + + if (fabs(r3[2]) > fabs(r2[2])) SWAP_ROWS(r3, r2); + if (0.0f == r2[2]) return false; + + m3 = r3[2] / r2[2]; + r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4], + r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], + r3[7] -= m3 * r2[7]; + + if (0.0f == r3[3]) return false; + + s = 1.0f / r3[3]; + r3[4] *= s; + r3[5] *= s; + r3[6] *= s; + r3[7] *= s; + + m2 = r2[3]; + s = 1.0f / r2[2]; + r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2), + r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2); + m1 = r1[3]; + r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1, + r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1; + m0 = r0[3]; + r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0, + r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0; + + m1 = r1[2]; + s = 1.0f / r1[1]; + r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1), + r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1); + m0 = r0[2]; + r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0, + r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0; + + m0 = r0[1]; + s = 1.0f / r0[0]; + r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0), + r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0); + + MAT(out, 0, 0) = r0[4]; + MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6]; + MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4]; + MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6]; + MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4]; + MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6]; + MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4]; + MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6]; + MAT(out, 3, 3) = r3[7]; + + return true; #undef MAT #undef SWAP_ROWS } void Matrix4x4_CreateFromEntity(float out[4][4], const vec3_t angles, const vec3_t origin, float scale) { - float angle, sr, sp, sy, cr, cp, cy; + float angle, sr, sp, sy, cr, cp, cy; #define PITCH 0 -#define YAW 1 -#define ROLL 2 - - if (angles[ROLL]) - { - angle = angles[YAW] * (M_PI * 2 / 360.0f); - SinCos(angle, &sy, &cy); - angle = angles[PITCH] * (M_PI * 2 / 360.0f); - SinCos(angle, &sp, &cp); - angle = angles[ROLL] * (M_PI * 2 / 360.0f); - SinCos(angle, &sr, &cr); - - out[0][0] = (cp*cy) * scale; - out[0][1] = (sr*sp*cy + cr * -sy) * scale; - out[0][2] = (cr*sp*cy + -sr * -sy) * scale; - out[0][3] = origin[0]; - out[1][0] = (cp*sy) * scale; - out[1][1] = (sr*sp*sy + cr * cy) * scale; - out[1][2] = (cr*sp*sy + -sr * cy) * scale; - out[1][3] = origin[1]; - out[2][0] = (-sp) * scale; - out[2][1] = (sr*cp) * scale; - out[2][2] = (cr*cp) * scale; - out[2][3] = origin[2]; - } - else if (angles[PITCH]) - { - angle = angles[YAW] * (M_PI * 2 / 360.0f); - SinCos(angle, &sy, &cy); - angle = angles[PITCH] * (M_PI * 2 / 360.0f); - SinCos(angle, &sp, &cp); - - out[0][0] = (cp*cy) * scale; - out[0][1] = (-sy) * scale; - out[0][2] = (sp*cy) * scale; - out[0][3] = origin[0]; - out[1][0] = (cp*sy) * scale; - out[1][1] = (cy)* scale; - out[1][2] = (sp*sy) * scale; - out[1][3] = origin[1]; - out[2][0] = (-sp) * scale; - out[2][1] = 0.0f; - out[2][2] = (cp)* scale; - out[2][3] = origin[2]; - } - else if (angles[YAW]) - { - angle = angles[YAW] * (M_PI * 2 / 360.0f); - SinCos(angle, &sy, &cy); - - out[0][0] = (cy)* scale; - out[0][1] = (-sy) * scale; - out[0][2] = 0.0f; - out[0][3] = origin[0]; - out[1][0] = (sy)* scale; - out[1][1] = (cy)* scale; - out[1][2] = 0.0f; - out[1][3] = origin[1]; - out[2][0] = 0.0f; - out[2][1] = 0.0f; - out[2][2] = scale; - out[2][3] = origin[2]; - } - else - { - out[0][0] = scale; - out[0][1] = 0.0f; - out[0][2] = 0.0f; - out[0][3] = origin[0]; - out[1][0] = 0.0f; - out[1][1] = scale; - out[1][2] = 0.0f; - out[1][3] = origin[1]; - out[2][0] = 0.0f; - out[2][1] = 0.0f; - out[2][2] = scale; - out[2][3] = origin[2]; - } +#define YAW 1 +#define ROLL 2 + + if (angles[ROLL]) + { + angle = angles[YAW] * (M_PI * 2 / 360.0f); + SinCos(angle, &sy, &cy); + angle = angles[PITCH] * (M_PI * 2 / 360.0f); + SinCos(angle, &sp, &cp); + angle = angles[ROLL] * (M_PI * 2 / 360.0f); + SinCos(angle, &sr, &cr); + + out[0][0] = (cp * cy) * scale; + out[0][1] = (sr * sp * cy + cr * -sy) * scale; + out[0][2] = (cr * sp * cy + -sr * -sy) * scale; + out[0][3] = origin[0]; + out[1][0] = (cp * sy) * scale; + out[1][1] = (sr * sp * sy + cr * cy) * scale; + out[1][2] = (cr * sp * sy + -sr * cy) * scale; + out[1][3] = origin[1]; + out[2][0] = (-sp) * scale; + out[2][1] = (sr * cp) * scale; + out[2][2] = (cr * cp) * scale; + out[2][3] = origin[2]; + } + else if (angles[PITCH]) + { + angle = angles[YAW] * (M_PI * 2 / 360.0f); + SinCos(angle, &sy, &cy); + angle = angles[PITCH] * (M_PI * 2 / 360.0f); + SinCos(angle, &sp, &cp); + + out[0][0] = (cp * cy) * scale; + out[0][1] = (-sy) * scale; + out[0][2] = (sp * cy) * scale; + out[0][3] = origin[0]; + out[1][0] = (cp * sy) * scale; + out[1][1] = (cy)*scale; + out[1][2] = (sp * sy) * scale; + out[1][3] = origin[1]; + out[2][0] = (-sp) * scale; + out[2][1] = 0.0f; + out[2][2] = (cp)*scale; + out[2][3] = origin[2]; + } + else if (angles[YAW]) + { + angle = angles[YAW] * (M_PI * 2 / 360.0f); + SinCos(angle, &sy, &cy); + + out[0][0] = (cy)*scale; + out[0][1] = (-sy) * scale; + out[0][2] = 0.0f; + out[0][3] = origin[0]; + out[1][0] = (sy)*scale; + out[1][1] = (cy)*scale; + out[1][2] = 0.0f; + out[1][3] = origin[1]; + out[2][0] = 0.0f; + out[2][1] = 0.0f; + out[2][2] = scale; + out[2][3] = origin[2]; + } + else + { + out[0][0] = scale; + out[0][1] = 0.0f; + out[0][2] = 0.0f; + out[0][3] = origin[0]; + out[1][0] = 0.0f; + out[1][1] = scale; + out[1][2] = 0.0f; + out[1][3] = origin[1]; + out[2][0] = 0.0f; + out[2][1] = 0.0f; + out[2][2] = scale; + out[2][3] = origin[2]; + } } void Matrix4x4_ConcatTransforms(float out[4][4], float in1[4][4], float in2[4][4]) { - out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0]; - out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1]; - out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2]; - out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3]; - out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0]; - out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1]; - out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2]; - out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3]; - out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0]; - out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1]; - out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2]; - out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3]; + out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0]; + out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1]; + out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2]; + out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3]; + out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0]; + out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1]; + out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2]; + out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3]; + out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0]; + out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1]; + out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2]; + out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3]; } void Matrix4x4_Transpose(float out[4][4], float in1[4][4]) { - out[0][0] = in1[0][0]; - out[0][1] = in1[1][0]; - out[0][2] = in1[2][0]; - out[0][3] = in1[3][0]; - out[1][0] = in1[0][1]; - out[1][1] = in1[1][1]; - out[1][2] = in1[2][1]; - out[1][3] = in1[3][1]; - out[2][0] = in1[0][2]; - out[2][1] = in1[1][2]; - out[2][2] = in1[2][2]; - out[2][3] = in1[3][2]; - out[3][0] = in1[0][3]; - out[3][1] = in1[1][3]; - out[3][2] = in1[2][3]; - out[3][3] = in1[3][3]; + out[0][0] = in1[0][0]; + out[0][1] = in1[1][0]; + out[0][2] = in1[2][0]; + out[0][3] = in1[3][0]; + out[1][0] = in1[0][1]; + out[1][1] = in1[1][1]; + out[1][2] = in1[2][1]; + out[1][3] = in1[3][1]; + out[2][0] = in1[0][2]; + out[2][1] = in1[1][2]; + out[2][2] = in1[2][2]; + out[2][3] = in1[3][2]; + out[3][0] = in1[0][3]; + out[3][1] = in1[1][3]; + out[3][2] = in1[2][3]; + out[3][3] = in1[3][3]; } \ No newline at end of file diff --git a/src/mathlib2.h b/src/mathlib2.h index 9bda0a3..8f94fd9 100644 --- a/src/mathlib2.h +++ b/src/mathlib2.h @@ -12,66 +12,86 @@ typedef int fixed8_t; typedef int fixed16_t; #define SIDE_FRONT 0 -#define SIDE_ON 2 -#define SIDE_BACK 1 +#define SIDE_ON 2 +#define SIDE_BACK 1 #define SIDE_CROSS -2 #ifndef M_PI -#define M_PI 3.14159265358979323846 +# define M_PI 3.14159265358979323846 #endif #define Q_PI 3.14159265358979323846 extern vec3_t vec3_origin; -#define EQUAL_EPSILON 0.001 +#define EQUAL_EPSILON 0.001 #define COLINEAR_EPSILON 0.001 struct mplane_s; extern int nanmask; -#define IS_NAN(x) (((*(int *)&x)&nanmask) == nanmask) +#define IS_NAN(x) (((*(int*)&x) & nanmask) == nanmask) #define METERS_PER_INCH 0.0254f -#define METER2INCH(x) (float)(x * (1.0f / METERS_PER_INCH)) -#define INCH2METER(x) (float)(x * (METERS_PER_INCH / 1.0f)) - -#define VectorSubtract(a, b, c) { (c)[0] = (a)[0] - (b)[0]; (c)[1] = (a)[1] - (b)[1]; (c)[2] = (a)[2] - (b)[2]; } -#define VectorAdd(a, b, c) { (c)[0] = (a)[0] + (b)[0]; (c)[1] = (a)[1] + (b)[1]; (c)[2] = (a)[2] + (b)[2]; } -#define VectorCopy(a, b) { (b)[0] = (a)[0]; (b)[1] = (a)[1]; (b)[2] = (a)[2]; } -#define VectorClear(a) { (a)[0] = 0.0; (a)[1] = 0.0; (a)[2] = 0.0; } -#define DotProduct(x, y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2]) -#define DotProduct4(x, y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2]+ (x)[3] * (y)[3]) +#define METER2INCH(x) (float)(x * (1.0f / METERS_PER_INCH)) +#define INCH2METER(x) (float)(x * (METERS_PER_INCH / 1.0f)) + +#define VectorSubtract(a, b, c) \ + { \ + (c)[0] = (a)[0] - (b)[0]; \ + (c)[1] = (a)[1] - (b)[1]; \ + (c)[2] = (a)[2] - (b)[2]; \ + } +#define VectorAdd(a, b, c) \ + { \ + (c)[0] = (a)[0] + (b)[0]; \ + (c)[1] = (a)[1] + (b)[1]; \ + (c)[2] = (a)[2] + (b)[2]; \ + } +#define VectorCopy(a, b) \ + { \ + (b)[0] = (a)[0]; \ + (b)[1] = (a)[1]; \ + (b)[2] = (a)[2]; \ + } +#define VectorClear(a) \ + { \ + (a)[0] = 0.0; \ + (a)[1] = 0.0; \ + (a)[2] = 0.0; \ + } +#define DotProduct(x, y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2]) +#define DotProduct4(x, y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2] + (x)[3] * (y)[3]) void MatrixCopy(float in[4][3], float out[4][3]); void VectorMA(const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc); vec_t _DotProduct(vec3_t v1, vec3_t v2); -void _VectorSubtract(vec3_t veca, vec3_t vecb, vec3_t out); -void _VectorAdd(vec3_t veca, vec3_t vecb, vec3_t out); -void _VectorCopy(vec3_t in, vec3_t out); +void _VectorSubtract(vec3_t veca, vec3_t vecb, vec3_t out); +void _VectorAdd(vec3_t veca, vec3_t vecb, vec3_t out); +void _VectorCopy(vec3_t in, vec3_t out); -int VectorCompare(const vec3_t v1, const vec3_t v2); +int VectorCompare(const vec3_t v1, const vec3_t v2); double VectorDistance(const vec3_t v1, const vec3_t v2); double VectorLength(vec3_t v); -void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross); -float VectorNormalize(vec3_t v); -void VectorInverse(vec3_t v); -void VectorScale(const vec3_t in, vec_t scale, vec3_t out); -int Q_log2(int val); -void ClearBounds(vec3_t mins, vec3_t maxs); +void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross); +float VectorNormalize(vec3_t v); +void VectorInverse(vec3_t v); +void VectorScale(const vec3_t in, vec_t scale, vec3_t out); +int Q_log2(int val); +void ClearBounds(vec3_t mins, vec3_t maxs); void R_ConcatRotations(float in1[3][3], float in2[3][3], float out[3][3]); void R_ConcatTransforms(float in1[3][4], float in2[3][4], float out[3][4]); void Matrix4x4_CreateFromEntity(float out[4][4], const vec3_t angles, const vec3_t origin, float scale); -void FloorDivMod(double numer, double denom, int *quotient, int *rem); -int GreatestCommonDivisor(int i1, int i2); +void FloorDivMod(double numer, double denom, int* quotient, int* rem); +int GreatestCommonDivisor(int i1, int i2); void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees); void AngleVectors(const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up); -void AngleVectorsTranspose(const vec3_t angles, vec3_t *forward, vec3_t *right, vec3_t *up); +void AngleVectorsTranspose(const vec3_t angles, vec3_t* forward, vec3_t* right, vec3_t* up); #define AngleIVectors AngleVectorsTranspose @@ -79,46 +99,41 @@ void AngleMatrix(const vec3_t angles, float (*matrix)[4]); void AngleIMatrix(const vec3_t angles, float (*matrix)[4]); void VectorTransform(const vec3_t in1, float in2[3][4], vec3_t out); -void NormalizeAngles(vec3_t angles); -void InterpolateAngles(vec3_t start, vec3_t end, vec3_t output, float frac); +void NormalizeAngles(vec3_t angles); +void InterpolateAngles(vec3_t start, vec3_t end, vec3_t output, float frac); float AngleBetweenVectors(const vec3_t v1, const vec3_t v2); void VectorMatrix(vec3_t forward, vec3_t right, vec3_t up); void VectorAngles(const vec3_t forward, vec3_t angles); -int InvertMatrix(const float * m, float *out); +int InvertMatrix(const float* m, float* out); void Matrix4x4_ConcatTransforms(float out[4][4], float in1[4][4], float in2[4][4]); void Matrix4x4_Transpose(float out[4][4], float in1[4][4]); -int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, struct mplane_s *plane); +int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, struct mplane_s* plane); void VectorRotate(const vec3_t in1, const float in2[3][4], vec3_t out); void VectorIRotate(const vec3_t in1, const float in2[3][4], vec3_t out); inline void SinCos(float radians, float* sine, float* cosine) { - *sine = sinf(radians); - *cosine = cosf(radians); + *sine = sinf(radians); + *cosine = cosf(radians); } inline float anglemod(float a) { - a = (360.f / 65536) * ((int)(a * (65536.f / 360.0f)) & 65535); - return a; + a = (360.f / 65536) * ((int)(a * (65536.f / 360.0f)) & 65535); + return a; } -#define BOX_ON_PLANE_SIDE(emins, emaxs, p) \ - (((p)->type < 3) ? \ - ( \ - ((p)->dist <= (emins)[(p)->type]) ? \ - 1 \ - : \ - ( \ - ((p)->dist >= (emaxs)[(p)->type]) ? \ - 2 \ - : \ - 3 \ - ) \ - ) \ - : \ - BoxOnPlaneSide((emins), (emaxs), (p))) \ No newline at end of file +#define BOX_ON_PLANE_SIDE(emins, emaxs, p) \ + (((p)->type < 3) ? \ + ( \ + ((p)->dist <= (emins)[(p)->type]) ? \ + 1 : \ + ( \ + ((p)->dist >= (emaxs)[(p)->type]) ? \ + 2 : \ + 3)) : \ + BoxOnPlaneSide((emins), (emaxs), (p))) \ No newline at end of file diff --git a/src/plugins.cpp b/src/plugins.cpp index 39c6404..9396f15 100644 --- a/src/plugins.cpp +++ b/src/plugins.cpp @@ -3,115 +3,113 @@ #include "exportfuncs.h" #include "privatehook.h" -cl_exportfuncs_t gExportfuncs = { 0 }; -mh_interface_t* g_pInterface = NULL; -metahook_api_t* g_pMetaHookAPI = NULL; -mh_enginesave_t* g_pMetaSave = NULL; -IFileSystem* g_pFileSystem = NULL; +cl_exportfuncs_t gExportfuncs = {0}; +mh_interface_t* g_pInterface = NULL; +metahook_api_t* g_pMetaHookAPI = NULL; +mh_enginesave_t* g_pMetaSave = NULL; +IFileSystem* g_pFileSystem = NULL; IFileSystem_HL25* g_pFileSystem_HL25 = NULL; -int g_iEngineType = 0; +int g_iEngineType = 0; DWORD g_dwEngineBuildnum = 0; -mh_dll_info_t g_EngineDLLInfo = { 0 }; -mh_dll_info_t g_MirrorEngineDLLInfo = { 0 }; -mh_dll_info_t g_ClientDLLInfo = { 0 }; -mh_dll_info_t g_MirrorClientDLLInfo = { 0 }; +mh_dll_info_t g_EngineDLLInfo = {0}; +mh_dll_info_t g_MirrorEngineDLLInfo = {0}; +mh_dll_info_t g_ClientDLLInfo = {0}; +mh_dll_info_t g_MirrorClientDLLInfo = {0}; -void IPluginsV4::Init(metahook_api_t *pAPI, mh_interface_t *pInterface, mh_enginesave_t *pSave) +void IPluginsV4::Init(metahook_api_t* pAPI, mh_interface_t* pInterface, mh_enginesave_t* pSave) { - g_pInterface = pInterface; - g_pMetaHookAPI = pAPI; - g_pMetaSave = pSave; + g_pInterface = pInterface; + g_pMetaHookAPI = pAPI; + g_pMetaSave = pSave; } void IPluginsV4::Shutdown(void) { } -void IPluginsV4::LoadEngine(cl_enginefunc_t *pEngfuncs) +void IPluginsV4::LoadEngine(cl_enginefunc_t* pEngfuncs) { - g_pFileSystem = g_pInterface->FileSystem; + g_pFileSystem = g_pInterface->FileSystem; - if (!g_pFileSystem) - { - g_pFileSystem_HL25 = g_pInterface->FileSystem_HL25; - } + if (!g_pFileSystem) + { + g_pFileSystem_HL25 = g_pInterface->FileSystem_HL25; + } - g_iEngineType = g_pMetaHookAPI->GetEngineType(); - g_dwEngineBuildnum = g_pMetaHookAPI->GetEngineBuildnum(); + g_iEngineType = g_pMetaHookAPI->GetEngineType(); + g_dwEngineBuildnum = g_pMetaHookAPI->GetEngineBuildnum(); - g_EngineDLLInfo.ImageBase = g_pMetaHookAPI->GetEngineBase(); - g_EngineDLLInfo.ImageSize = g_pMetaHookAPI->GetEngineSize(); - g_EngineDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_EngineDLLInfo.ImageBase, ".text\x0\x0\x0", &g_EngineDLLInfo.TextSize); - g_EngineDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_EngineDLLInfo.ImageBase, ".data\x0\x0\x0", &g_EngineDLLInfo.DataSize); - g_EngineDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_EngineDLLInfo.ImageBase, ".rdata\x0\x0", &g_EngineDLLInfo.RdataSize); + g_EngineDLLInfo.ImageBase = g_pMetaHookAPI->GetEngineBase(); + g_EngineDLLInfo.ImageSize = g_pMetaHookAPI->GetEngineSize(); + g_EngineDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_EngineDLLInfo.ImageBase, ".text\x0\x0\x0", &g_EngineDLLInfo.TextSize); + g_EngineDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_EngineDLLInfo.ImageBase, ".data\x0\x0\x0", &g_EngineDLLInfo.DataSize); + g_EngineDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_EngineDLLInfo.ImageBase, ".rdata\x0\x0", &g_EngineDLLInfo.RdataSize); - g_MirrorEngineDLLInfo.ImageBase = g_pMetaHookAPI->GetMirrorEngineBase(); - g_MirrorEngineDLLInfo.ImageSize = g_pMetaHookAPI->GetMirrorEngineSize(); + g_MirrorEngineDLLInfo.ImageBase = g_pMetaHookAPI->GetMirrorEngineBase(); + g_MirrorEngineDLLInfo.ImageSize = g_pMetaHookAPI->GetMirrorEngineSize(); - if (g_MirrorEngineDLLInfo.ImageBase) - { - g_MirrorEngineDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_MirrorEngineDLLInfo.ImageBase, ".text\x0\x0\x0", &g_MirrorEngineDLLInfo.TextSize); - g_MirrorEngineDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorEngineDLLInfo.ImageBase, ".data\x0\x0\x0", &g_MirrorEngineDLLInfo.DataSize); - g_MirrorEngineDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorEngineDLLInfo.ImageBase, ".rdata\x0\x0", &g_MirrorEngineDLLInfo.RdataSize); - } + if (g_MirrorEngineDLLInfo.ImageBase) + { + g_MirrorEngineDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_MirrorEngineDLLInfo.ImageBase, ".text\x0\x0\x0", &g_MirrorEngineDLLInfo.TextSize); + g_MirrorEngineDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorEngineDLLInfo.ImageBase, ".data\x0\x0\x0", &g_MirrorEngineDLLInfo.DataSize); + g_MirrorEngineDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorEngineDLLInfo.ImageBase, ".rdata\x0\x0", &g_MirrorEngineDLLInfo.RdataSize); + } - memcpy(&gEngfuncs, pEngfuncs, sizeof(gEngfuncs)); + memcpy(&gEngfuncs, pEngfuncs, sizeof(gEngfuncs)); } -void IPluginsV4::LoadClient(cl_exportfuncs_t *pExportFunc) +void IPluginsV4::LoadClient(cl_exportfuncs_t* pExportFunc) { - memcpy(&gExportfuncs, pExportFunc, sizeof(gExportfuncs)); - - g_ClientDLLInfo.ImageBase = g_pMetaHookAPI->GetClientBase(); - g_ClientDLLInfo.ImageSize = g_pMetaHookAPI->GetClientSize(); - g_ClientDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_ClientDLLInfo.ImageBase, ".text\0\0\0", &g_ClientDLLInfo.TextSize); - g_ClientDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_ClientDLLInfo.ImageBase, ".data\0\0\0", &g_ClientDLLInfo.DataSize); - g_ClientDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_ClientDLLInfo.ImageBase, ".rdata\0\0", &g_ClientDLLInfo.RdataSize); - - g_MirrorClientDLLInfo.ImageBase = g_pMetaHookAPI->GetMirrorClientBase(); - g_MirrorClientDLLInfo.ImageSize = g_pMetaHookAPI->GetMirrorClientSize(); - - if (g_MirrorClientDLLInfo.ImageBase) - { - g_MirrorClientDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_MirrorClientDLLInfo.ImageBase, ".text\0\0\0", &g_MirrorClientDLLInfo.TextSize); - g_MirrorClientDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorClientDLLInfo.ImageBase, ".data\0\0\0", &g_MirrorClientDLLInfo.DataSize); - g_MirrorClientDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorClientDLLInfo.ImageBase, ".rdata\0\0", &g_MirrorClientDLLInfo.RdataSize); - } - - pExportFunc->CAM_Think = CAM_Think; - pExportFunc->HUD_Init = HUD_Init; - pExportFunc->HUD_Shutdown = HUD_Shutdown; - - Client_FillAddress(g_MirrorClientDLLInfo.ImageBase ? g_MirrorClientDLLInfo : g_ClientDLLInfo, g_ClientDLLInfo); - Client_InstallHooks(); + memcpy(&gExportfuncs, pExportFunc, sizeof(gExportfuncs)); + + g_ClientDLLInfo.ImageBase = g_pMetaHookAPI->GetClientBase(); + g_ClientDLLInfo.ImageSize = g_pMetaHookAPI->GetClientSize(); + g_ClientDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_ClientDLLInfo.ImageBase, ".text\0\0\0", &g_ClientDLLInfo.TextSize); + g_ClientDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_ClientDLLInfo.ImageBase, ".data\0\0\0", &g_ClientDLLInfo.DataSize); + g_ClientDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_ClientDLLInfo.ImageBase, ".rdata\0\0", &g_ClientDLLInfo.RdataSize); + + g_MirrorClientDLLInfo.ImageBase = g_pMetaHookAPI->GetMirrorClientBase(); + g_MirrorClientDLLInfo.ImageSize = g_pMetaHookAPI->GetMirrorClientSize(); + + if (g_MirrorClientDLLInfo.ImageBase) + { + g_MirrorClientDLLInfo.TextBase = g_pMetaHookAPI->GetSectionByName(g_MirrorClientDLLInfo.ImageBase, ".text\0\0\0", &g_MirrorClientDLLInfo.TextSize); + g_MirrorClientDLLInfo.DataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorClientDLLInfo.ImageBase, ".data\0\0\0", &g_MirrorClientDLLInfo.DataSize); + g_MirrorClientDLLInfo.RdataBase = g_pMetaHookAPI->GetSectionByName(g_MirrorClientDLLInfo.ImageBase, ".rdata\0\0", &g_MirrorClientDLLInfo.RdataSize); + } + + pExportFunc->CAM_Think = CAM_Think; + pExportFunc->HUD_Init = HUD_Init; + pExportFunc->HUD_Shutdown = HUD_Shutdown; + + Client_FillAddress(g_MirrorClientDLLInfo.ImageBase ? g_MirrorClientDLLInfo : g_ClientDLLInfo, g_ClientDLLInfo); + Client_InstallHooks(); } void IPluginsV4::ExitGame(int iResult) { - } const char completeVersion[] = + { + BUILD_YEAR_CH0, BUILD_YEAR_CH1, BUILD_YEAR_CH2, BUILD_YEAR_CH3, + '-', + BUILD_MONTH_CH0, BUILD_MONTH_CH1, + '-', + BUILD_DAY_CH0, BUILD_DAY_CH1, + 'T', + BUILD_HOUR_CH0, BUILD_HOUR_CH1, + ':', + BUILD_MIN_CH0, BUILD_MIN_CH1, + ':', + BUILD_SEC_CH0, BUILD_SEC_CH1, + '\0'}; + +const char* IPluginsV4::GetVersion(void) { - BUILD_YEAR_CH0, BUILD_YEAR_CH1, BUILD_YEAR_CH2, BUILD_YEAR_CH3, - '-', - BUILD_MONTH_CH0, BUILD_MONTH_CH1, - '-', - BUILD_DAY_CH0, BUILD_DAY_CH1, - 'T', - BUILD_HOUR_CH0, BUILD_HOUR_CH1, - ':', - BUILD_MIN_CH0, BUILD_MIN_CH1, - ':', - BUILD_SEC_CH0, BUILD_SEC_CH1, - '\0' -}; - -const char *IPluginsV4::GetVersion(void) -{ - return completeVersion; + return completeVersion; } EXPOSE_SINGLE_INTERFACE(IPluginsV4, IPluginsV4, METAHOOK_PLUGIN_API_VERSION_V4); \ No newline at end of file diff --git a/src/plugins.h b/src/plugins.h index da79147..b30694a 100644 --- a/src/plugins.h +++ b/src/plugins.h @@ -10,10 +10,10 @@ static_assert(METAHOOK_API_VERSION >= 109, "SCCameraFix resolves client symbols class IFileSystem; class IFileSystem_HL25; -extern IFileSystem* g_pFileSystem; +extern IFileSystem* g_pFileSystem; extern IFileSystem_HL25* g_pFileSystem_HL25; -extern int g_iEngineType; +extern int g_iEngineType; extern DWORD g_dwEngineBuildnum; extern mh_dll_info_t g_EngineDLLInfo; @@ -21,46 +21,57 @@ extern mh_dll_info_t g_MirrorEngineDLLInfo; extern mh_dll_info_t g_ClientDLLInfo; extern mh_dll_info_t g_MirrorClientDLLInfo; -#define MHPluginName "SCCameraFix" -#define Sys_Error(msg, ...) g_pMetaHookAPI->SysError("[" MHPluginName "] " msg, __VA_ARGS__); -#define Sig_NotFound(name) Sys_Error("Could not found: %s\nEngine buildnum: %d", #name, g_dwEngineBuildnum); -#define Sig_VarNotFound(name) if(!name) Sig_NotFound(name) -#define Sig_AddrNotFound(name) if(!addr) Sig_NotFound(name) -#define Sig_FuncNotFound(name) if(!gPrivateFuncs.name) Sig_NotFound(name) +#define MHPluginName "SCCameraFix" +#define Sys_Error(msg, ...) g_pMetaHookAPI->SysError("[" MHPluginName "] " msg, __VA_ARGS__); +#define Sig_NotFound(name) Sys_Error("Could not found: %s\nEngine buildnum: %d", #name, g_dwEngineBuildnum); +#define Sig_VarNotFound(name) \ + if (!name) Sig_NotFound(name) +#define Sig_AddrNotFound(name) \ + if (!addr) Sig_NotFound(name) +#define Sig_FuncNotFound(name) \ + if (!gPrivateFuncs.name) Sig_NotFound(name) //Resolve a required gamedata symbol; a missing symbol is fatal, mirroring the //Sig_VarNotFound policy of the signature-scan locators. //moduleName identifies the module owning the symbol and is echoed by the diagnostic. inline PVOID GamedataResolvePtr(PVOID moduleBase, const char* moduleName, const char* symbolName, mh_gamesymbol_kind_t kind) { - PVOID address = nullptr; - mh_gamesymbol_status_t status = g_pMetaHookAPI->ResolveGameSymbol(moduleBase, symbolName, kind, &address); + PVOID address = nullptr; + mh_gamesymbol_status_t status = g_pMetaHookAPI->ResolveGameSymbol(moduleBase, symbolName, kind, &address); - if (status != MH_GAMESYMBOL_OK) - { - Sys_Error("Could not resolve gamedata symbol: %s (module %s, %s)\nEngine buildnum: %d", - symbolName, moduleName, g_pMetaHookAPI->GetGameSymbolStatusString(status), g_dwEngineBuildnum); - } + if (status != MH_GAMESYMBOL_OK) + { + Sys_Error("Could not resolve gamedata symbol: %s (module %s, %s)\nEngine buildnum: %d", + symbolName, moduleName, g_pMetaHookAPI->GetGameSymbolStatusString(status), g_dwEngineBuildnum); + } - return address; + return address; } -#define Sig_Length(a) (sizeof(a)-1) -#define Search_Pattern(sig, dllinfo) g_pMetaHookAPI->SearchPattern(dllinfo.TextBase, dllinfo.TextSize, sig, Sig_Length(sig)) -#define Search_Pattern_Data(sig, dllinfo) g_pMetaHookAPI->SearchPattern(dllinfo.DataBase, dllinfo.DataSize, sig, Sig_Length(sig)) -#define Search_Pattern_Rdata(sig, dllinfo) g_pMetaHookAPI->SearchPattern(dllinfo.RdataBase, dllinfo.RdataSize, sig, Sig_Length(sig)) -#define Search_Pattern_From_Size(fn, size, sig) g_pMetaHookAPI->SearchPattern((void *)(fn), size, sig, Sig_Length(sig)) -#define Search_Pattern_From(fn, sig, dllinfo) g_pMetaHookAPI->SearchPattern((void *)(fn), ((PUCHAR)dllinfo.TextBase + dllinfo.TextSize) - (PUCHAR)(fn), sig, Sig_Length(sig)) +#define Sig_Length(a) (sizeof(a) - 1) +#define Search_Pattern(sig, dllinfo) g_pMetaHookAPI->SearchPattern(dllinfo.TextBase, dllinfo.TextSize, sig, Sig_Length(sig)) +#define Search_Pattern_Data(sig, dllinfo) g_pMetaHookAPI->SearchPattern(dllinfo.DataBase, dllinfo.DataSize, sig, Sig_Length(sig)) +#define Search_Pattern_Rdata(sig, dllinfo) g_pMetaHookAPI->SearchPattern(dllinfo.RdataBase, dllinfo.RdataSize, sig, Sig_Length(sig)) +#define Search_Pattern_From_Size(fn, size, sig) g_pMetaHookAPI->SearchPattern((void*)(fn), size, sig, Sig_Length(sig)) +#define Search_Pattern_From(fn, sig, dllinfo) g_pMetaHookAPI->SearchPattern((void*)(fn), ((PUCHAR)dllinfo.TextBase + dllinfo.TextSize) - (PUCHAR)(fn), sig, Sig_Length(sig)) -#define Search_Pattern_NoWildCard(sig, dllinfo) g_pMetaHookAPI->SearchPatternNoWildCard(dllinfo.TextBase, dllinfo.TextSize, sig, Sig_Length(sig)) -#define Search_Pattern_NoWildCard_Data(sig, dllinfo) g_pMetaHookAPI->SearchPatternNoWildCard(dllinfo.DataBase, dllinfo.DataSize, sig, Sig_Length(sig)) +#define Search_Pattern_NoWildCard(sig, dllinfo) g_pMetaHookAPI->SearchPatternNoWildCard(dllinfo.TextBase, dllinfo.TextSize, sig, Sig_Length(sig)) +#define Search_Pattern_NoWildCard_Data(sig, dllinfo) g_pMetaHookAPI->SearchPatternNoWildCard(dllinfo.DataBase, dllinfo.DataSize, sig, Sig_Length(sig)) #define Search_Pattern_NoWildCard_Rdata(sig, dllinfo) g_pMetaHookAPI->SearchPatternNoWildCard(dllinfo.RdataBase, dllinfo.RdataSize, sig, Sig_Length(sig)) -#define Install_InlineHook(fn) if(!g_phook_##fn) { g_phook_##fn = g_pMetaHookAPI->InlineHook((void *)gPrivateFuncs.fn, fn, (void **)&gPrivateFuncs.fn); } -#define Uninstall_Hook(fn) if(g_phook_##fn){g_pMetaHookAPI->UnHook(g_phook_##fn);g_phook_##fn = NULL;} +#define Install_InlineHook(fn) \ + if (!g_phook_##fn) { g_phook_##fn = g_pMetaHookAPI->InlineHook((void*)gPrivateFuncs.fn, fn, (void**)&gPrivateFuncs.fn); } +#define Uninstall_Hook(fn) \ + if (g_phook_##fn) \ + { \ + g_pMetaHookAPI->UnHook(g_phook_##fn); \ + g_phook_##fn = NULL; \ + } #define GetCallAddress(addr) g_pMetaHookAPI->GetNextCallAddr((PUCHAR)addr, 1) #define RVA_from_VA(name, dllinfo) (ULONG)((ULONG_PTR)name##_VA - (ULONG_PTR)dllinfo.ImageBase) #define VA_from_RVA(name, dllinfo) ((ULONG_PTR)dllinfo.ImageBase + (ULONG_PTR)name##_RVA) -#define Convert_VA_to_RVA(name, dllinfo) if(name##_VA) name##_RVA = ((ULONG_PTR)name##_VA - (ULONG_PTR)dllinfo.ImageBase) -#define Convert_RVA_to_VA(name, dllinfo) if(name##_RVA) name##_VA = (decltype(name##_VA))VA_from_RVA(name, dllinfo) +#define Convert_VA_to_RVA(name, dllinfo) \ + if (name##_VA) name##_RVA = ((ULONG_PTR)name##_VA - (ULONG_PTR)dllinfo.ImageBase) +#define Convert_RVA_to_VA(name, dllinfo) \ + if (name##_RVA) name##_VA = (decltype(name##_VA))VA_from_RVA(name, dllinfo) diff --git a/src/privatehook.cpp b/src/privatehook.cpp index 19e472d..50febe3 100644 --- a/src/privatehook.cpp +++ b/src/privatehook.cpp @@ -2,7 +2,7 @@ #include "plugins.h" #include "privatehook.h" -vec3_t* v_origin = NULL; +vec3_t* v_origin = NULL; vec3_t* g_vVecViewangles = NULL; int* g_iUser1 = NULL; @@ -10,89 +10,89 @@ int* g_iUser2 = NULL; struct event_api_s** g_pClientDLLEventAPI = NULL; -private_funcs_t gPrivateFuncs = { 0 }; +private_funcs_t gPrivateFuncs = {0}; static hook_t* g_phook_V_CalcNormalRefdef = NULL; void Client_FillAddress_CL_IsThirdPerson(const mh_dll_info_t& DllInfo, const mh_dll_info_t& RealDllInfo) { - //The old locator disassembled the client's exported CL_IsThirdPerson and - //picked the last two adjacent .data references out of it; the catalog - //publishes those two globals directly for both SvEngine clients. - g_iUser1 = (decltype(g_iUser1))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "g_iUser1", MH_GAMESYMBOL_KIND_GLOBAL); - g_iUser2 = (decltype(g_iUser2))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "g_iUser2", MH_GAMESYMBOL_KIND_GLOBAL); + //The old locator disassembled the client's exported CL_IsThirdPerson and + //picked the last two adjacent .data references out of it; the catalog + //publishes those two globals directly for both SvEngine clients. + g_iUser1 = (decltype(g_iUser1))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "g_iUser1", MH_GAMESYMBOL_KIND_GLOBAL); + g_iUser2 = (decltype(g_iUser2))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "g_iUser2", MH_GAMESYMBOL_KIND_GLOBAL); } void Client_FillAddress(const mh_dll_info_t& DllInfo, const mh_dll_info_t& RealDllInfo) { - auto pfnClientFactory = g_pMetaHookAPI->GetClientFactory(); - - if (pfnClientFactory && pfnClientFactory("SCClientDLL001", 0)) - { - //The old locator disassembled the client's `mov eax,[gEngfuncs]; mov - //eax,[eax+EventAPI]; jmp eax` sequence and took the loaded .data operand - //as the EventAPI slot; the catalog publishes the `gEngfuncs` global, so - //the slot is its `pEventAPI` member. - auto pEngfuncs = (cl_enginefunc_t*)GamedataResolvePtr(RealDllInfo.ImageBase, "client", "gEngfuncs", MH_GAMESYMBOL_KIND_GLOBAL); - g_pClientDLLEventAPI = &pEngfuncs->pEventAPI; - } - else - { - Sys_Error("This plugin is for Sven Co-op!"); - return; - } - - v_origin = (decltype(v_origin))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "v_origin", MH_GAMESYMBOL_KIND_GLOBAL); - - gPrivateFuncs.V_CalcNormalRefdef = (decltype(gPrivateFuncs.V_CalcNormalRefdef))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "V_CalcNormalRefdef", MH_GAMESYMBOL_KIND_FUNCTION); - g_vVecViewangles = (decltype(g_vVecViewangles))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "g_vVecViewangles", MH_GAMESYMBOL_KIND_GLOBAL); - - Client_FillAddress_CL_IsThirdPerson(DllInfo, RealDllInfo); + auto pfnClientFactory = g_pMetaHookAPI->GetClientFactory(); + + if (pfnClientFactory && pfnClientFactory("SCClientDLL001", 0)) + { + //The old locator disassembled the client's `mov eax,[gEngfuncs]; mov + //eax,[eax+EventAPI]; jmp eax` sequence and took the loaded .data operand + //as the EventAPI slot; the catalog publishes the `gEngfuncs` global, so + //the slot is its `pEventAPI` member. + auto pEngfuncs = (cl_enginefunc_t*)GamedataResolvePtr(RealDllInfo.ImageBase, "client", "gEngfuncs", MH_GAMESYMBOL_KIND_GLOBAL); + g_pClientDLLEventAPI = &pEngfuncs->pEventAPI; + } + else + { + Sys_Error("This plugin is for Sven Co-op!"); + return; + } + + v_origin = (decltype(v_origin))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "v_origin", MH_GAMESYMBOL_KIND_GLOBAL); + + gPrivateFuncs.V_CalcNormalRefdef = (decltype(gPrivateFuncs.V_CalcNormalRefdef))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "V_CalcNormalRefdef", MH_GAMESYMBOL_KIND_FUNCTION); + g_vVecViewangles = (decltype(g_vVecViewangles))GamedataResolvePtr(RealDllInfo.ImageBase, "client", "g_vVecViewangles", MH_GAMESYMBOL_KIND_GLOBAL); + + Client_FillAddress_CL_IsThirdPerson(DllInfo, RealDllInfo); } void Client_InstallHooks(void) { - Install_InlineHook(V_CalcNormalRefdef); + Install_InlineHook(V_CalcNormalRefdef); } void Client_UninstallHooks(void) { - Uninstall_Hook(V_CalcNormalRefdef); + Uninstall_Hook(V_CalcNormalRefdef); } PVOID ConvertDllInfoSpace(PVOID addr, const mh_dll_info_t& SrcDllInfo, const mh_dll_info_t& TargetDllInfo) { - if ((ULONG_PTR)addr > (ULONG_PTR)SrcDllInfo.ImageBase && (ULONG_PTR)addr < (ULONG_PTR)SrcDllInfo.ImageBase + SrcDllInfo.ImageSize) - { - auto addr_VA = (ULONG_PTR)addr; - auto addr_RVA = RVA_from_VA(addr, SrcDllInfo); + if ((ULONG_PTR)addr > (ULONG_PTR)SrcDllInfo.ImageBase && (ULONG_PTR)addr < (ULONG_PTR)SrcDllInfo.ImageBase + SrcDllInfo.ImageSize) + { + auto addr_VA = (ULONG_PTR)addr; + auto addr_RVA = RVA_from_VA(addr, SrcDllInfo); - return (PVOID)VA_from_RVA(addr, TargetDllInfo); - } + return (PVOID)VA_from_RVA(addr, TargetDllInfo); + } - return nullptr; + return nullptr; } PVOID GetVFunctionFromVFTable(PVOID* vftable, int index, const mh_dll_info_t& DllInfo, const mh_dll_info_t& RealDllInfo, const mh_dll_info_t& OutputDllInfo) { - if ((ULONG_PTR)vftable > (ULONG_PTR)RealDllInfo.ImageBase && (ULONG_PTR)vftable < (ULONG_PTR)RealDllInfo.ImageBase + RealDllInfo.ImageSize) - { - ULONG_PTR vftable_VA = (ULONG_PTR)vftable; - ULONG vftable_RVA = RVA_from_VA(vftable, RealDllInfo); - auto vftable_DllInfo = (decltype(vftable))VA_from_RVA(vftable, DllInfo); - - auto vf_VA = (ULONG_PTR)vftable_DllInfo[index]; - ULONG vf_RVA = RVA_from_VA(vf, DllInfo); - - return (PVOID)VA_from_RVA(vf, OutputDllInfo); - } - else if ((ULONG_PTR)vftable > (ULONG_PTR)DllInfo.ImageBase && (ULONG_PTR)vftable < (ULONG_PTR)DllInfo.ImageBase + DllInfo.ImageSize) - { - auto vf_VA = (ULONG_PTR)vftable[index]; - ULONG vf_RVA = RVA_from_VA(vf, DllInfo); - - return (PVOID)VA_from_RVA(vf, OutputDllInfo); - } - - return vftable[index]; + if ((ULONG_PTR)vftable > (ULONG_PTR)RealDllInfo.ImageBase && (ULONG_PTR)vftable < (ULONG_PTR)RealDllInfo.ImageBase + RealDllInfo.ImageSize) + { + ULONG_PTR vftable_VA = (ULONG_PTR)vftable; + ULONG vftable_RVA = RVA_from_VA(vftable, RealDllInfo); + auto vftable_DllInfo = (decltype(vftable))VA_from_RVA(vftable, DllInfo); + + auto vf_VA = (ULONG_PTR)vftable_DllInfo[index]; + ULONG vf_RVA = RVA_from_VA(vf, DllInfo); + + return (PVOID)VA_from_RVA(vf, OutputDllInfo); + } + else if ((ULONG_PTR)vftable > (ULONG_PTR)DllInfo.ImageBase && (ULONG_PTR)vftable < (ULONG_PTR)DllInfo.ImageBase + DllInfo.ImageSize) + { + auto vf_VA = (ULONG_PTR)vftable[index]; + ULONG vf_RVA = RVA_from_VA(vf, DllInfo); + + return (PVOID)VA_from_RVA(vf, OutputDllInfo); + } + + return vftable[index]; } diff --git a/src/privatehook.h b/src/privatehook.h index 4e7bbb3..de863e9 100644 --- a/src/privatehook.h +++ b/src/privatehook.h @@ -4,8 +4,8 @@ typedef struct { - void (*V_CalcNormalRefdef)(ref_params_t*); -}private_funcs_t; + void (*V_CalcNormalRefdef)(ref_params_t*); +} private_funcs_t; extern private_funcs_t gPrivateFuncs;