From 9d1b5cf6043a2dff66326d282ab53b7d72a2f515 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 7 Nov 2021 22:48:52 +0100 Subject: [PATCH 01/27] Better r_draw debug functions, and relocated in renderer --- src/Components/Modules/QuickPatch.cpp | 198 ---------------------- src/Components/Modules/Renderer.cpp | 227 ++++++++++++++++++++++++++ src/Components/Modules/Renderer.hpp | 12 ++ 3 files changed, 239 insertions(+), 198 deletions(-) diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index 1ab8479b..87c906da 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -964,204 +964,6 @@ namespace Components } }); - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawAabbTrees").get()) return; - - float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - - Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); - //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); - if (!clipMap) return; - - for (unsigned short i = 0; i < clipMap->smodelNodeCount; ++i) - { - Game::R_AddDebugBounds(cyan, &clipMap->smodelNodes[i].bounds); - } - - for (unsigned int i = 0; i < clipMap->numStaticModels; i += 2) - { - Game::R_AddDebugBounds(red, &clipMap->staticModelList[i].absBounds); - } - }); - - Dvar::OnInit([] - { - Dvar::Register("r_drawSceneModelBoundingBoxes", false, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Dvar::Register("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Dvar::Register("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Dvar::Register("r_drawModelNames", false, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Dvar::Register("r_drawAabbTrees", false, Game::DVAR_FLAG_USERCREATED, "Draw aabb trees"); - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawModelNames").get()) return; - - float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; - float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; - - auto mapName = Dvar::Var("mapname").get(); - auto* scene = Game::scene; - auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; - - for (auto i = 0; i < scene->sceneModelCount; i++) - { - if (!scene->sceneModel[i].model) - continue; - - Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); - } - - for (auto i = 0; i < scene->sceneDObjCount; i++) - { - if (scene->sceneDObj[i].obj) { - for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) - { - Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); - } - } - } - - // Static models - for (size_t i = 0; i < world->dpvs.smodelCount; i++) - { - auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (staticModel.model) { - Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); - } - } - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawSceneModelBoundingBoxes").get()) return; - - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float blue[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; - - auto* scene = Game::scene; - - for(auto i = 0; i < scene->sceneModelCount; i++) - { - if(!scene->sceneModel[i].model) - continue; - - auto b = scene->sceneModel[i].model->bounds; - b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; - b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; - b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; - b.halfSize[0] *= scene->sceneModel[i].placement.scale; - b.halfSize[1] *= scene->sceneModel[i].placement.scale; - b.halfSize[2] *= scene->sceneModel[i].placement.scale; - Game::R_AddDebugBounds(red, &b, &scene->sceneModel[i].placement.base.quat); - } - - for(auto i = 0; i < scene->sceneDObjCount; i++) - { - scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); - scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); - scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); - - if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) { - - Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); - } - - Game::R_AddDebugBounds(blue, &scene->sceneDObj[i].cull.bounds); - } - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized()) return; - if (!Dvar::Var("r_drawSceneModelCollisions").get()) return; - - float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; - - auto* scene = Game::scene; - - for (auto i = 0; i < scene->sceneModelCount; i++) - { - if (!scene->sceneModel[i].model) - continue; - - for (auto j = 0; j < scene->sceneModel[i].model->numCollSurfs; j++) { - auto b = scene->sceneModel[i].model->collSurfs[j].bounds; - b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; - b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; - b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; - b.halfSize[0] *= scene->sceneModel[i].placement.scale; - b.halfSize[1] *= scene->sceneModel[i].placement.scale; - b.halfSize[2] *= scene->sceneModel[i].placement.scale; - - Game::R_AddDebugBounds(green, &b, &scene->sceneModel[i].placement.base.quat); - } - } - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawTriggers").get()) return; - - float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; - float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; - float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; - - auto* entities = Game::g_entities; - for(auto i = 0u; i < 0x800; i++) - { - auto* ent = &entities[i]; - - if(ent->r.isInUse) - { - Game::Bounds b = ent->r.box; - b.midPoint[0] += ent->r.currentOrigin[0]; - b.midPoint[1] += ent->r.currentOrigin[1]; - b.midPoint[2] += ent->r.currentOrigin[2]; - - switch(ent->handler) - { - case Game::ENT_HANDLER_TRIGGER_HURT: - Game::R_AddDebugBounds(hurt, &b); - break; - - case Game::ENT_HANDLER_TRIGGER_HURT_TOUCH: - Game::R_AddDebugBounds(hurtTouch, &b); - break; - - case Game::ENT_HANDLER_TRIGGER_DAMAGE: - Game::R_AddDebugBounds(damage, &b); - break; - - case Game::ENT_HANDLER_TRIGGER_MULTIPLE: - if(ent->spawnflags & 0x40) - Game::R_AddDebugBounds(once, &b); - else - Game::R_AddDebugBounds(multiple, &b); - break; - - default: - float rv = std::min((float)ent->handler, (float)5) / 5; - float gv = std::clamp((float)ent->handler-5, (float)0, (float)5) / 5; - float bv = std::clamp((float)ent->handler - 10, (float)0, (float)5) / 5; - - float color[4] = { rv, gv, bv, 1.0f }; - - Game::R_AddDebugBounds(color, &b); - break; - } - } - } - }); - - // Dvars Dvar::Register("ui_streamFriendly", false, Game::DVAR_FLAG_SAVED, "Stream friendly UI"); diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 2422fde2..be2d8240 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -8,6 +8,12 @@ namespace Components Utils::Signal Renderer::EndRecoverDeviceSignal; Utils::Signal Renderer::BeginRecoverDeviceSignal; + Dvar::Var Renderer::DrawTriggers; + Dvar::Var Renderer::DrawSceneModelCollisions; + Dvar::Var Renderer::DrawSceneModelBoundingBoxes; + Dvar::Var Renderer::DrawModelNames; + Dvar::Var Renderer::DrawAABBTrees; + __declspec(naked) void Renderer::FrameStub() { __asm @@ -166,6 +172,209 @@ namespace Components return Utils::Hook::Call(0x005033E0)(a1, a2, a3, Utils::String::VA("%s (^3%s^7)", mat->info.name, mat->techniqueSet->name), color, a6); } + void Renderer::DebugDrawTriggers() + { + if (!DrawTriggers.get()) return; + + float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; + float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + + auto* entities = Game::g_entities; + + for (auto i = 0u; i < 0x800; i++) + { + auto* ent = &entities[i]; + + if (ent->r.isInUse) + { + Game::Bounds b = ent->r.box; + b.midPoint[0] += ent->r.currentOrigin[0]; + b.midPoint[1] += ent->r.currentOrigin[1]; + b.midPoint[2] += ent->r.currentOrigin[2]; + + switch (ent->handler) + { + case Game::ENT_HANDLER_TRIGGER_HURT: + Game::R_AddDebugBounds(hurt, &b); + break; + + case Game::ENT_HANDLER_TRIGGER_HURT_TOUCH: + Game::R_AddDebugBounds(hurtTouch, &b); + break; + + case Game::ENT_HANDLER_TRIGGER_DAMAGE: + Game::R_AddDebugBounds(damage, &b); + break; + + case Game::ENT_HANDLER_TRIGGER_MULTIPLE: + if (ent->spawnflags & 0x40) + Game::R_AddDebugBounds(once, &b); + else + Game::R_AddDebugBounds(multiple, &b); + break; + + default: + float rv = std::min((float)ent->handler, (float)5) / 5; + float gv = std::clamp((float)ent->handler - 5, (float)0, (float)5) / 5; + float bv = std::clamp((float)ent->handler - 10, (float)0, (float)5) / 5; + + float color[4] = { rv, gv, bv, 1.0f }; + + Game::R_AddDebugBounds(color, &b); + break; + } + } + } + } + + void Renderer::DebugDrawSceneModelCollisions() + { + if (!DrawSceneModelCollisions.get()) return; + + float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + + auto* scene = Game::scene; + + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; + + for (auto j = 0; j < scene->sceneModel[i].model->numCollSurfs; j++) + { + auto b = scene->sceneModel[i].model->collSurfs[j].bounds; + b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; + b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; + b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; + b.halfSize[0] *= scene->sceneModel[i].placement.scale; + b.halfSize[1] *= scene->sceneModel[i].placement.scale; + b.halfSize[2] *= scene->sceneModel[i].placement.scale; + + Game::R_AddDebugBounds(green, &b, &scene->sceneModel[i].placement.base.quat); + } + } + } + + void Renderer::DebugDrawSceneModelBoundingBoxes() + { + if (!DrawSceneModelBoundingBoxes.get()) return; + + float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float blue[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + + auto* scene = Game::scene; + + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; + + auto b = scene->sceneModel[i].model->bounds; + b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; + b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; + b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; + b.halfSize[0] *= scene->sceneModel[i].placement.scale; + b.halfSize[1] *= scene->sceneModel[i].placement.scale; + b.halfSize[2] *= scene->sceneModel[i].placement.scale; + Game::R_AddDebugBounds(red, &b, &scene->sceneModel[i].placement.base.quat); + } + + for (auto i = 0; i < scene->sceneDObjCount; i++) + { + scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); + scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); + scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); + + if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) + { + + Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); + } + + Game::R_AddDebugBounds(blue, &scene->sceneDObj[i].cull.bounds); + } + } + + void Renderer::DebugDrawModelNames() + { + auto val = DrawModelNames.get()->current.integer; + + if (!val) return; + + float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; + float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; + + auto mapName = Dvar::Var("mapname").get(); + auto* scene = Game::scene; + auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + + if (val == 1) + { + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; + + Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); + } + } + + else if (val == 2) + { + for (auto i = 0; i < scene->sceneDObjCount; i++) + { + if (scene->sceneDObj[i].obj) + { + for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) + { + Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); + } + } + } + } + + else if (val == 3) + { + // Static models + for (size_t i = 0; i < world->dpvs.smodelCount; i++) + { + auto staticModel = world->dpvs.smodelDrawInsts[i]; + if (staticModel.model) + { + Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); + } + } + } + } + + void Renderer::DebugDrawAABBTrees() + { + if (!DrawAABBTrees.get()) return; + + float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; + float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + + Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); + //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); + if (!clipMap) return; + + for (unsigned short i = 0; i < clipMap->smodelNodeCount; ++i) + { + Game::R_AddDebugBounds(cyan, &clipMap->smodelNodes[i].bounds); + } + + for (unsigned int i = 0; i < clipMap->numStaticModels; i += 2) + { + Game::R_AddDebugBounds(red, &clipMap->staticModelList[i].absBounds); + } + } + Renderer::Renderer() { if (Dedicated::IsEnabled()) return; @@ -230,6 +439,24 @@ namespace Components // End vid_restart Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); + + Dvar::OnInit([] + { + static std::vector < char* > values = + { + const_cast("Disabled"), + const_cast("Scene Models"), + const_cast("Scene Dynamic Objects"), + const_cast("GfxWorld Static Models"), + nullptr + }; + + Renderer::DrawSceneModelBoundingBoxes = Game::Dvar_RegisterBool("r_drawSceneModelBoundingBoxes", false, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterBool("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + }); } Renderer::~Renderer() diff --git a/src/Components/Modules/Renderer.hpp b/src/Components/Modules/Renderer.hpp index 040eb6ba..48b5a0e6 100644 --- a/src/Components/Modules/Renderer.hpp +++ b/src/Components/Modules/Renderer.hpp @@ -35,10 +35,22 @@ namespace Components static int DrawTechsetForMaterial(int a1, float a2, float a3, const char* material, Game::vec4_t* color, int a6); + static void DebugDrawTriggers(); + static void DebugDrawSceneModelCollisions(); + static void DebugDrawSceneModelBoundingBoxes(); + static void DebugDrawModelNames(); + static void DebugDrawAABBTrees(); + static Utils::Signal EndRecoverDeviceSignal; static Utils::Signal BeginRecoverDeviceSignal; static Utils::Signal BackendFrameSignal; static Utils::Signal SingleBackendFrameSignal; + + static Dvar::Var DrawTriggers; + static Dvar::Var DrawSceneModelCollisions; + static Dvar::Var DrawSceneModelBoundingBoxes; + static Dvar::Var DrawModelNames; + static Dvar::Var DrawAABBTrees; }; } From 15a3f0fe040b1ec73ea02325c4fc487c21a45aca Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 7 Nov 2021 22:57:35 +0100 Subject: [PATCH 02/27] Actually call the functions --- src/Components/Modules/Renderer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index be2d8240..f789e02a 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -379,6 +379,17 @@ namespace Components { if (Dedicated::IsEnabled()) return; + Scheduler::OnFrame([]() { + if (Game::CL_IsCgameInitialized()) + { + DebugDrawAABBTrees(); + DebugDrawModelNames(); + DebugDrawSceneModelBoundingBoxes(); + DebugDrawSceneModelCollisions(); + DebugDrawTriggers(); + } + }); + // Renderer::OnBackendFrame([] (IDirect3DDevice9* device) // { // if (Game::Sys_Milliseconds() % 2) From 0c87243519e7adbe10d7868886b89fb4db76f9b9 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 17:55:48 +0100 Subject: [PATCH 03/27] Vec3 Square distance function --- src/Utils/Utils.cpp | 7 +++++++ src/Utils/Utils.hpp | 1 + 2 files changed, 8 insertions(+) diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 3a16ee58..299bfa6c 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -149,4 +149,11 @@ namespace Utils { return !(base1 + len1 <= base2 || base2 + len2 <= base1); } + + float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) + { + return (std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); + } + + } diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index f296f0e1..257ec5bf 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -24,6 +24,7 @@ namespace Utils void OpenUrl(const std::string& url); bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2); + float Vec3SqrDistance(float (*v1)[3], float (*v2)[3]); template inline void RotLeft(T& object, size_t bits) { From b63cdc6632c5fa7c69a460418f37ddeddcfad7ec Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 17:56:19 +0100 Subject: [PATCH 04/27] Much better r_drawmodelnames and r_drawboundingboxes for every model category --- src/Components/Modules/Renderer.cpp | 277 +++++++++++++++++----------- src/Components/Modules/Renderer.hpp | 4 +- 2 files changed, 172 insertions(+), 109 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index f789e02a..e4463488 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -10,10 +10,16 @@ namespace Components Dvar::Var Renderer::DrawTriggers; Dvar::Var Renderer::DrawSceneModelCollisions; - Dvar::Var Renderer::DrawSceneModelBoundingBoxes; + Dvar::Var Renderer::DrawModelBoundingBoxes; Dvar::Var Renderer::DrawModelNames; Dvar::Var Renderer::DrawAABBTrees; + float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; + float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; + float gentitiesColor[4] = { 1.0f, 0.5f, 0.5f, 1.0f }; + int drawDistance = 80000; + __declspec(naked) void Renderer::FrameStub() { __asm @@ -133,7 +139,7 @@ namespace Components // show error pushad; - push [esp + 24h + 20h]; + push[esp + 24h + 20h]; push eax; call R_TextureFromCodeError; add esp, 8; @@ -258,45 +264,90 @@ namespace Components } } - void Renderer::DebugDrawSceneModelBoundingBoxes() + void Renderer::DebugDrawModelBoundingBoxes() { - if (!DrawSceneModelBoundingBoxes.get()) return; + auto val = DrawModelBoundingBoxes.get()->current.integer; - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float blue[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + if (!val) return; + int clientNum = Game::CG_GetClientNum(); + if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + + Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; + + float(*playerPosition)[3] = &clientEntity->r.currentOrigin; + + auto mapName = Dvar::Var("mapname").get(); auto* scene = Game::scene; + auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; - for (auto i = 0; i < scene->sceneModelCount; i++) - { - if (!scene->sceneModel[i].model) - continue; + switch (val) { + case 1: + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; - auto b = scene->sceneModel[i].model->bounds; - b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; - b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; - b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; - b.halfSize[0] *= scene->sceneModel[i].placement.scale; - b.halfSize[1] *= scene->sceneModel[i].placement.scale; - b.halfSize[2] *= scene->sceneModel[i].placement.scale; - Game::R_AddDebugBounds(red, &b, &scene->sceneModel[i].placement.base.quat); - } + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + { + auto b = scene->sceneModel[i].model->bounds; + b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; + b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; + b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; + b.halfSize[0] *= scene->sceneModel[i].placement.scale; + b.halfSize[1] *= scene->sceneModel[i].placement.scale; + b.halfSize[2] *= scene->sceneModel[i].placement.scale; + Game::R_AddDebugBounds(sceneModelsColor, &b, &scene->sceneModel[i].placement.base.quat); + } + } + break; - for (auto i = 0; i < scene->sceneDObjCount; i++) - { - scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); - scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); - scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); - - if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) + case 2: + for (auto i = 0; i < scene->sceneDObjCount; i++) { - Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); - } + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < drawDistance) + { + scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); + scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); + scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); - Game::R_AddDebugBounds(blue, &scene->sceneDObj[i].cull.bounds); + if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) + { + + Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); + } + + Game::R_AddDebugBounds(dobjsColor, &scene->sceneDObj[i].cull.bounds); + } + } + break; + + case 3: + // Static models + for (size_t i = 0; i < world->dpvs.smodelCount; i++) + { + auto staticModel = world->dpvs.smodelDrawInsts[i]; + + if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < drawDistance) + { + if (staticModel.model) + { + Game::Bounds b = staticModel.model->bounds; + b.midPoint[0] += staticModel.placement.origin[0]; + b.midPoint[1] += staticModel.placement.origin[1]; + b.midPoint[2] += staticModel.placement.origin[2]; + b.halfSize[0] *= staticModel.placement.scale; + b.halfSize[1] *= staticModel.placement.scale; + b.halfSize[2] *= staticModel.placement.scale; + + Game::R_AddDebugBounds(staticModelsColor, &b); + } + } + } + break; } } @@ -306,50 +357,62 @@ namespace Components if (!val) return; - float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; - float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; + int clientNum = Game::CG_GetClientNum(); + if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + + Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; + + float (*playerPosition)[3] = &clientEntity->r.currentOrigin; auto mapName = Dvar::Var("mapname").get(); auto* scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; - if (val == 1) - { + switch (val) { + case 1: for (auto i = 0; i < scene->sceneModelCount; i++) { if (!scene->sceneModel[i].model) continue; - Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + { + Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); + } } - } + break; - else if (val == 2) - { + case 2: for (auto i = 0; i < scene->sceneDObjCount; i++) { if (scene->sceneDObj[i].obj) { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < drawDistance) + { + Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); + } } } } - } + break; - else if (val == 3) - { + case 3: // Static models for (size_t i = 0; i < world->dpvs.smodelCount; i++) { auto staticModel = world->dpvs.smodelDrawInsts[i]; if (staticModel.model) { - Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); + auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); + if (dist < drawDistance) + { + Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); + } } } + break; } } @@ -384,41 +447,41 @@ namespace Components { DebugDrawAABBTrees(); DebugDrawModelNames(); - DebugDrawSceneModelBoundingBoxes(); + DebugDrawModelBoundingBoxes(); DebugDrawSceneModelCollisions(); DebugDrawTriggers(); } }); -// Renderer::OnBackendFrame([] (IDirect3DDevice9* device) -// { -// if (Game::Sys_Milliseconds() % 2) -// { -// device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, 0, 0, 0); -// } -// -// return; -// -// IDirect3DSurface9* buffer = nullptr; -// -// device->CreateOffscreenPlainSurface(Renderer::Width(), Renderer::Height(), D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &buffer, nullptr); -// device->GetFrontBufferData(0, buffer); -// -// if (buffer) -// { -// D3DSURFACE_DESC desc; -// D3DLOCKED_RECT lockedRect; -// -// buffer->GetDesc(&desc); -// -// HRESULT res = buffer->LockRect(&lockedRect, NULL, D3DLOCK_READONLY); -// -// -// buffer->UnlockRect(); -// } -// }); + // Renderer::OnBackendFrame([] (IDirect3DDevice9* device) + // { + // if (Game::Sys_Milliseconds() % 2) + // { + // device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, 0, 0, 0); + // } + // + // return; + // + // IDirect3DSurface9* buffer = nullptr; + // + // device->CreateOffscreenPlainSurface(Renderer::Width(), Renderer::Height(), D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &buffer, nullptr); + // device->GetFrontBufferData(0, buffer); + // + // if (buffer) + // { + // D3DSURFACE_DESC desc; + // D3DLOCKED_RECT lockedRect; + // + // buffer->GetDesc(&desc); + // + // HRESULT res = buffer->LockRect(&lockedRect, NULL, D3DLOCK_READONLY); + // + // + // buffer->UnlockRect(); + // } + // }); - // Log broken materials + // Log broken materials Utils::Hook(0x0054CAAA, Renderer::StoreGfxBufContextPtrStub1, HOOK_JUMP).install()->quick(); Utils::Hook(0x0054CF8D, Renderer::StoreGfxBufContextPtrStub2, HOOK_JUMP).install()->quick(); @@ -433,41 +496,41 @@ namespace Components // Begin device recovery (not D3D9Ex) Utils::Hook(0x508298, []() - { - Game::DB_BeginRecoverLostDevice(); - Renderer::BeginRecoverDeviceSignal(); - }, HOOK_CALL).install()->quick(); - - // End device recovery (not D3D9Ex) - Utils::Hook(0x508355, []() - { - Renderer::EndRecoverDeviceSignal(); - Game::DB_EndRecoverLostDevice(); - }, HOOK_CALL).install()->quick(); - - // Begin vid_restart - Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); - - // End vid_restart - Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); - - Dvar::OnInit([] { - static std::vector < char* > values = - { - const_cast("Disabled"), - const_cast("Scene Models"), - const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models"), - nullptr - }; + Game::DB_BeginRecoverLostDevice(); + Renderer::BeginRecoverDeviceSignal(); + }, HOOK_CALL).install()->quick(); - Renderer::DrawSceneModelBoundingBoxes = Game::Dvar_RegisterBool("r_drawSceneModelBoundingBoxes", false, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterBool("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - }); + // End device recovery (not D3D9Ex) + Utils::Hook(0x508355, []() + { + Renderer::EndRecoverDeviceSignal(); + Game::DB_EndRecoverLostDevice(); + }, HOOK_CALL).install()->quick(); + + // Begin vid_restart + Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); + + // End vid_restart + Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); + + Dvar::OnInit([] + { + static std::vector < char* > values = + { + const_cast("Disabled"), + const_cast("Scene Models"), + const_cast("Scene Dynamic Objects"), + const_cast("GfxWorld Static Models") + nullptr + }; + + Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + }); } Renderer::~Renderer() diff --git a/src/Components/Modules/Renderer.hpp b/src/Components/Modules/Renderer.hpp index 48b5a0e6..8773fd3a 100644 --- a/src/Components/Modules/Renderer.hpp +++ b/src/Components/Modules/Renderer.hpp @@ -37,7 +37,7 @@ namespace Components static void DebugDrawTriggers(); static void DebugDrawSceneModelCollisions(); - static void DebugDrawSceneModelBoundingBoxes(); + static void DebugDrawModelBoundingBoxes(); static void DebugDrawModelNames(); static void DebugDrawAABBTrees(); @@ -49,7 +49,7 @@ namespace Components static Dvar::Var DrawTriggers; static Dvar::Var DrawSceneModelCollisions; - static Dvar::Var DrawSceneModelBoundingBoxes; + static Dvar::Var DrawModelBoundingBoxes; static Dvar::Var DrawModelNames; static Dvar::Var DrawAABBTrees; }; From 08054af027d07236537e5817d8d53733837994c7 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 18:05:49 +0100 Subject: [PATCH 05/27] Whoopsies --- src/Components/Modules/Renderer.cpp | 2 +- src/Utils/Utils.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index e4463488..7ea81832 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -521,7 +521,7 @@ namespace Components const_cast("Disabled"), const_cast("Scene Models"), const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models") + const_cast("GfxWorld Static Models"), nullptr }; diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 299bfa6c..8a7a7cb8 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -152,7 +152,7 @@ namespace Utils float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) { - return (std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); + return static_cast(std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); } From b57a7800511ba6245b4970d2e0f5383501fff9f9 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 19:00:51 +0100 Subject: [PATCH 06/27] Fixed formatting a little bit --- src/Components/Modules/Renderer.cpp | 56 ++++++++++++++--------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 7ea81832..815521b5 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -362,7 +362,7 @@ namespace Components Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float (*playerPosition)[3] = &clientEntity->r.currentOrigin; + float(*playerPosition)[3] = &clientEntity->r.currentOrigin; auto mapName = Dvar::Var("mapname").get(); auto* scene = Game::scene; @@ -451,7 +451,7 @@ namespace Components DebugDrawSceneModelCollisions(); DebugDrawTriggers(); } - }); + }); // Renderer::OnBackendFrame([] (IDirect3DDevice9* device) // { @@ -501,36 +501,36 @@ namespace Components Renderer::BeginRecoverDeviceSignal(); }, HOOK_CALL).install()->quick(); - // End device recovery (not D3D9Ex) - Utils::Hook(0x508355, []() - { - Renderer::EndRecoverDeviceSignal(); - Game::DB_EndRecoverLostDevice(); - }, HOOK_CALL).install()->quick(); + // End device recovery (not D3D9Ex) + Utils::Hook(0x508355, []() + { + Renderer::EndRecoverDeviceSignal(); + Game::DB_EndRecoverLostDevice(); + }, HOOK_CALL).install()->quick(); - // Begin vid_restart - Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); + // Begin vid_restart + Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); - // End vid_restart - Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); + // End vid_restart + Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); - Dvar::OnInit([] - { - static std::vector < char* > values = - { - const_cast("Disabled"), - const_cast("Scene Models"), - const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models"), - nullptr - }; + Dvar::OnInit([] + { + static std::vector < char* > values = + { + const_cast("Disabled"), + const_cast("Scene Models"), + const_cast("Scene Dynamic Objects"), + const_cast("GfxWorld Static Models"), + nullptr + }; - Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - }); + Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + }); } Renderer::~Renderer() From 892f521728b7dad6641820cae0c2937811617c6c Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Thu, 11 Nov 2021 14:00:46 +0100 Subject: [PATCH 07/27] Added a dvar for player draw debug distance, use array for dvar enums, a bit of formatting --- src/Components/Modules/Renderer.cpp | 104 ++++++++++++++++------------ src/Components/Modules/Renderer.hpp | 11 +-- 2 files changed, 66 insertions(+), 49 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 815521b5..a1d01f90 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -8,17 +8,29 @@ namespace Components Utils::Signal Renderer::EndRecoverDeviceSignal; Utils::Signal Renderer::BeginRecoverDeviceSignal; - Dvar::Var Renderer::DrawTriggers; - Dvar::Var Renderer::DrawSceneModelCollisions; - Dvar::Var Renderer::DrawModelBoundingBoxes; - Dvar::Var Renderer::DrawModelNames; - Dvar::Var Renderer::DrawAABBTrees; + Dvar::Var Renderer::r_drawTriggers; + Dvar::Var Renderer::r_drawSceneModelCollisions; + Dvar::Var Renderer::r_drawModelBoundingBoxes; + Dvar::Var Renderer::r_drawModelNames; + Dvar::Var Renderer::r_drawAABBTrees; + Dvar::Var Renderer::r_playerDrawDebugDistance; + float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; + float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + + // R_draw model names & collisions colors float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; float gentitiesColor[4] = { 1.0f, 0.5f, 0.5f, 1.0f }; - int drawDistance = 80000; + + // Trigger colors + float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; + float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; __declspec(naked) void Renderer::FrameStub() { @@ -180,17 +192,11 @@ namespace Components void Renderer::DebugDrawTriggers() { - if (!DrawTriggers.get()) return; - - float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; - float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; - float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + if (!r_drawTriggers.get()) return; auto* entities = Game::g_entities; - for (auto i = 0u; i < 0x800; i++) + for (auto i = 0u; i < 0x800u; i++) { auto* ent = &entities[i]; @@ -223,9 +229,9 @@ namespace Components break; default: - float rv = std::min((float)ent->handler, (float)5) / 5; - float gv = std::clamp((float)ent->handler - 5, (float)0, (float)5) / 5; - float bv = std::clamp((float)ent->handler - 10, (float)0, (float)5) / 5; + auto rv = std::min(static_cast(ent->handler), 5.0f) / 5.0f; + auto gv = std::clamp(static_cast(ent->handler - 5), 0.f, 5.f) / 5.f; + auto bv = std::clamp(static_cast(ent->handler - 10), 0.f, 5.f) / 5.f; float color[4] = { rv, gv, bv, 1.0f }; @@ -238,9 +244,7 @@ namespace Components void Renderer::DebugDrawSceneModelCollisions() { - if (!DrawSceneModelCollisions.get()) return; - - float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + if (!r_drawSceneModelCollisions.get()) return; auto* scene = Game::scene; @@ -266,12 +270,19 @@ namespace Components void Renderer::DebugDrawModelBoundingBoxes() { - auto val = DrawModelBoundingBoxes.get()->current.integer; + auto val = r_drawModelBoundingBoxes.get(); if (!val) return; + // Ingame only int clientNum = Game::CG_GetClientNum(); - if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + if (!Game::CL_IsCgameInitialized() || + clientNum >= 18 || + clientNum < 0 || + !Game::g_entities[clientNum].client) { + + return; + } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; @@ -288,7 +299,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) { auto b = scene->sceneModel[i].model->bounds; b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; @@ -306,7 +317,7 @@ namespace Components for (auto i = 0; i < scene->sceneDObjCount; i++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < r_playerDrawDebugDistance.get()) { scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); @@ -331,7 +342,7 @@ namespace Components { auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < r_playerDrawDebugDistance.get()) { if (staticModel.model) { @@ -353,12 +364,19 @@ namespace Components void Renderer::DebugDrawModelNames() { - auto val = DrawModelNames.get()->current.integer; + auto val = r_drawModelNames.get(); if (!val) return; + // Ingame only int clientNum = Game::CG_GetClientNum(); - if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + if (!Game::CL_IsCgameInitialized() || + clientNum >= 18 || + clientNum < 0 || + !Game::g_entities[clientNum].client) { + + return; + } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; @@ -375,7 +393,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) { Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); } @@ -389,7 +407,7 @@ namespace Components { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < r_playerDrawDebugDistance.get()) { Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); } @@ -406,7 +424,7 @@ namespace Components if (staticModel.model) { auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); - if (dist < drawDistance) + if (dist < r_playerDrawDebugDistance.get()) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); } @@ -418,10 +436,7 @@ namespace Components void Renderer::DebugDrawAABBTrees() { - if (!DrawAABBTrees.get()) return; - - float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + if (!r_drawAABBTrees.get()) return; Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); @@ -516,20 +531,21 @@ namespace Components Dvar::OnInit([] { - static std::vector < char* > values = + char* values[5] = { - const_cast("Disabled"), - const_cast("Scene Models"), - const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models"), + "Disabled", + "Scene Models", + "Scene Dynamic Objects", + "GfxWorld Static Models", nullptr }; - Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + Renderer::r_drawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::r_drawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::r_drawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::r_drawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values, 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::r_drawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 100000, 0, 500000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); }); } diff --git a/src/Components/Modules/Renderer.hpp b/src/Components/Modules/Renderer.hpp index 8773fd3a..553dd336 100644 --- a/src/Components/Modules/Renderer.hpp +++ b/src/Components/Modules/Renderer.hpp @@ -47,10 +47,11 @@ namespace Components static Utils::Signal BackendFrameSignal; static Utils::Signal SingleBackendFrameSignal; - static Dvar::Var DrawTriggers; - static Dvar::Var DrawSceneModelCollisions; - static Dvar::Var DrawModelBoundingBoxes; - static Dvar::Var DrawModelNames; - static Dvar::Var DrawAABBTrees; + static Dvar::Var r_drawTriggers; + static Dvar::Var r_drawSceneModelCollisions; + static Dvar::Var r_drawModelBoundingBoxes; + static Dvar::Var r_drawModelNames; + static Dvar::Var r_drawAABBTrees; + static Dvar::Var r_playerDrawDebugDistance; }; } From 6d70cd3e743456530a2b645400a7d81b771bc9b6 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Thu, 11 Nov 2021 16:44:54 +0100 Subject: [PATCH 08/27] Get int now works on enum dvars --- src/Components/Modules/Dvar.cpp | 2 +- src/Components/Modules/Renderer.cpp | 22 ++++++++++++++-------- src/Utils/Utils.cpp | 6 +++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index d0892d2e..c8628e4d 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -38,7 +38,7 @@ namespace Components template <> int Dvar::Var::get() { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_INT) + if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_INT || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) { return this->dvar->current.integer; } diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index a1d01f90..2aa68b2d 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -292,6 +292,9 @@ namespace Components auto* scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + auto drawDistance = r_playerDrawDebugDistance.get(); + auto sqrDist = drawDistance * drawDistance; + switch (val) { case 1: for (auto i = 0; i < scene->sceneModelCount; i++) @@ -299,7 +302,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) { auto b = scene->sceneModel[i].model->bounds; b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; @@ -317,7 +320,7 @@ namespace Components for (auto i = 0; i < scene->sceneDObjCount; i++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < sqrDist) { scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); @@ -342,7 +345,7 @@ namespace Components { auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < sqrDist) { if (staticModel.model) { @@ -386,6 +389,9 @@ namespace Components auto* scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + auto drawDistance = r_playerDrawDebugDistance.get(); + auto sqrDist = drawDistance * drawDistance; + switch (val) { case 1: for (auto i = 0; i < scene->sceneModelCount; i++) @@ -393,7 +399,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) { Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); } @@ -407,7 +413,7 @@ namespace Components { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < sqrDist) { Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); } @@ -424,7 +430,7 @@ namespace Components if (staticModel.model) { auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); - if (dist < r_playerDrawDebugDistance.get()) + if (dist < sqrDist) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); } @@ -531,7 +537,7 @@ namespace Components Dvar::OnInit([] { - char* values[5] = + static char* values[5] = { "Disabled", "Scene Models", @@ -545,7 +551,7 @@ namespace Components Renderer::r_drawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); Renderer::r_drawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values, 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); Renderer::r_drawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 100000, 0, 500000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); + Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 1000, 0, 50000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); }); } diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 8a7a7cb8..5feb2980 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -152,7 +152,11 @@ namespace Utils float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) { - return static_cast(std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); + auto x = (*v2)[0] - (*v1)[0]; + auto y = (*v2)[1] - (*v1)[1]; + auto z = (*v2)[2] - (*v1)[2]; + + return x * x + y * y + z * z; } From d726015ba24ad2d045c2052935808a4add5071de Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 13 Dec 2021 18:46:55 +0100 Subject: [PATCH 09/27] Fix some declarations and pointer madness in Vec3SqrDist --- src/Components/Modules/Renderer.cpp | 35 +++++++++++++++-------------- src/Utils/Utils.cpp | 8 +++---- src/Utils/Utils.hpp | 2 +- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 2aa68b2d..ef957924 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -194,7 +194,7 @@ namespace Components { if (!r_drawTriggers.get()) return; - auto* entities = Game::g_entities; + auto entities = Game::g_entities; for (auto i = 0u; i < 0x800u; i++) { @@ -230,8 +230,8 @@ namespace Components default: auto rv = std::min(static_cast(ent->handler), 5.0f) / 5.0f; - auto gv = std::clamp(static_cast(ent->handler - 5), 0.f, 5.f) / 5.f; - auto bv = std::clamp(static_cast(ent->handler - 10), 0.f, 5.f) / 5.f; + auto gv = std::clamp(static_cast(ent->handler - 5), 0.f, 5.0f) / 5.0f; + auto bv = std::clamp(static_cast(ent->handler - 10), 0.f, 5.0f) / 5.0f; float color[4] = { rv, gv, bv, 1.0f }; @@ -246,7 +246,7 @@ namespace Components { if (!r_drawSceneModelCollisions.get()) return; - auto* scene = Game::scene; + auto scene = Game::scene; for (auto i = 0; i < scene->sceneModelCount; i++) { @@ -279,30 +279,31 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - !Game::g_entities[clientNum].client) { + Game::g_entities[clientNum].client != nullptr) { return; } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float(*playerPosition)[3] = &clientEntity->r.currentOrigin; + float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; auto mapName = Dvar::Var("mapname").get(); - auto* scene = Game::scene; + auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; auto drawDistance = r_playerDrawDebugDistance.get(); auto sqrDist = drawDistance * drawDistance; - switch (val) { + switch (val) + { case 1: for (auto i = 0; i < scene->sceneModelCount; i++) { if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < sqrDist) { auto b = scene->sceneModel[i].model->bounds; b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; @@ -320,7 +321,7 @@ namespace Components for (auto i = 0; i < scene->sceneDObjCount; i++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneDObj[i].cull.bounds.midPoint) < sqrDist) { scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); @@ -345,7 +346,7 @@ namespace Components { auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, staticModel.placement.origin) < sqrDist) { if (staticModel.model) { @@ -376,17 +377,17 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - !Game::g_entities[clientNum].client) { + Game::g_entities[clientNum].client != nullptr) { return; } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float(*playerPosition)[3] = &clientEntity->r.currentOrigin; + float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; auto mapName = Dvar::Var("mapname").get(); - auto* scene = Game::scene; + auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; auto drawDistance = r_playerDrawDebugDistance.get(); @@ -399,7 +400,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < sqrDist) { Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); } @@ -413,7 +414,7 @@ namespace Components { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneDObj[i].placement.origin) < sqrDist) { Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); } @@ -429,7 +430,7 @@ namespace Components auto staticModel = world->dpvs.smodelDrawInsts[i]; if (staticModel.model) { - auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); + auto dist = Utils::Vec3SqrDistance(playerPosition, staticModel.placement.origin); if (dist < sqrDist) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 5feb2980..71d0af14 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -150,11 +150,11 @@ namespace Utils return !(base1 + len1 <= base2 || base2 + len2 <= base1); } - float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) + float Vec3SqrDistance(const float v1[3], const float v2[3]) { - auto x = (*v2)[0] - (*v1)[0]; - auto y = (*v2)[1] - (*v1)[1]; - auto z = (*v2)[2] - (*v1)[2]; + auto x = v2[0] - v1[0]; + auto y = v2[1] - v1[1]; + auto z = v2[2] - v1[2]; return x * x + y * y + z * z; } diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index 257ec5bf..c33bbda3 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -24,7 +24,7 @@ namespace Utils void OpenUrl(const std::string& url); bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2); - float Vec3SqrDistance(float (*v1)[3], float (*v2)[3]); + float Vec3SqrDistance(const float v1[3], const float v2[3]); template inline void RotLeft(T& object, size_t bits) { From 6ec5cac5cbd5eb355aaf76760e4dc7e36272f65d Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 13 Dec 2021 18:50:52 +0100 Subject: [PATCH 10/27] Fix const cast on enum dvar registration --- src/Components/Modules/Renderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 321ab9fa..f7104751 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -538,7 +538,7 @@ namespace Components Dvar::OnInit([] { - static char* values[5] = + static const char* values[5] = { "Disabled", "Scene Models", From c0f95d3fc3be3a8cfeccbdc350d222627ff5d72d Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 13 Dec 2021 19:06:04 +0100 Subject: [PATCH 11/27] Address review --- src/Components/Modules/Renderer.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index f7104751..3d4b8f86 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -279,7 +279,7 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - Game::g_entities[clientNum].client != nullptr) { + Game::g_entities[clientNum].client == nullptr) { return; } @@ -288,7 +288,7 @@ namespace Components float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; - auto mapName = Dvar::Var("mapname").get(); + const auto mapName = Dvar::Var("mapname").get(); auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; @@ -377,7 +377,7 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - Game::g_entities[clientNum].client != nullptr) { + Game::g_entities[clientNum].client == nullptr) { return; } @@ -386,7 +386,7 @@ namespace Components float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; - auto mapName = Dvar::Var("mapname").get(); + const auto mapName = Dvar::Var("mapname").get(); auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; @@ -518,10 +518,10 @@ namespace Components // Begin device recovery (not D3D9Ex) Utils::Hook(0x508298, []() - { - Game::DB_BeginRecoverLostDevice(); - Renderer::BeginRecoverDeviceSignal(); - }, HOOK_CALL).install()->quick(); + { + Game::DB_BeginRecoverLostDevice(); + Renderer::BeginRecoverDeviceSignal(); + }, HOOK_CALL).install()->quick(); // End device recovery (not D3D9Ex) Utils::Hook(0x508355, []() @@ -538,7 +538,7 @@ namespace Components Dvar::OnInit([] { - static const char* values[5] = + static const char* values[] = { "Disabled", "Scene Models", From a6f0d78481cc7e8f140898833d97be1b5c43ce0b Mon Sep 17 00:00:00 2001 From: "rack.over" Date: Fri, 17 Dec 2021 09:55:50 +0100 Subject: [PATCH 12/27] address review --- src/Components/Modules/Renderer.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 3d4b8f86..07cc2a65 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -446,7 +446,6 @@ namespace Components if (!r_drawAABBTrees.get()) return; Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); - //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); if (!clipMap) return; for (unsigned short i = 0; i < clipMap->smodelNodeCount; ++i) @@ -548,11 +547,11 @@ namespace Components }; Renderer::r_drawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::r_drawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::r_drawSceneModelCollisions = Game::Dvar_RegisterBool("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); Renderer::r_drawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); Renderer::r_drawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values, 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); Renderer::r_drawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 1000, 0, 50000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); + Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugDistance", 1000, 0, 50000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); }); } From 25a79f8439e663ac72be04a320c88a4147dcf4ff Mon Sep 17 00:00:00 2001 From: Diavolo Date: Fri, 17 Dec 2021 19:49:25 +0100 Subject: [PATCH 13/27] Added missing lock guard --- src/Components/Modules/Bans.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Components/Modules/Bans.cpp b/src/Components/Modules/Bans.cpp index d3f5d306..6930eb28 100644 --- a/src/Components/Modules/Bans.cpp +++ b/src/Components/Modules/Bans.cpp @@ -6,6 +6,8 @@ namespace Components bool Bans::IsBanned(Bans::Entry entry) { + std::lock_guard _(Bans::AccessMutex); + Bans::BanList list; Bans::LoadBans(&list); From 8736983c465f97c2c448ee860e02ba688eaf7071 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Wed, 22 Dec 2021 16:25:58 +0100 Subject: [PATCH 14/27] Refactor elevators --- src/Components/Modules/Elevators.cpp | 135 +++++++++++++++++++++------ src/Components/Modules/Elevators.hpp | 7 +- src/Game/Functions.cpp | 6 ++ src/Game/Functions.hpp | 14 ++- src/Game/Structs.hpp | 113 ++++++++++++++++++++-- 5 files changed, 233 insertions(+), 42 deletions(-) diff --git a/src/Components/Modules/Elevators.cpp b/src/Components/Modules/Elevators.cpp index f297f48a..1d7aa12d 100644 --- a/src/Components/Modules/Elevators.cpp +++ b/src/Components/Modules/Elevators.cpp @@ -2,38 +2,107 @@ namespace Components { - Game::dvar_t* Elevators::SV_DisableElevators; + Dvar::Var Elevators::SV_Elevators; - __declspec(naked) void Elevators::PM_GroundTraceStub() + int Elevators::PM_CorrectAllSolid(Game::pmove_s* pm, Game::pml_t* pml, Game::trace_t* trace) + { + assert(pm != nullptr); + assert(pm->ps != nullptr); + + Game::vec3_t point; + auto* ps = pm->ps; + + auto i = 0; + const auto EleSettings = Elevators::SV_Elevators.get(); + while (TRUE) + { + point[0] = ps->origin[0] + Game::CorrectSolidDeltas[i][0]; + point[1] = ps->origin[1] + Game::CorrectSolidDeltas[i][1]; + point[2] = ps->origin[2] + Game::CorrectSolidDeltas[i][2]; + + Game::PM_playerTrace(pm, trace, point, point, &pm->bounds, ps->clientNum, pm->tracemask); + + // If the player wishes to glitch without effort they can do so + if (!trace->startsolid || EleSettings == Elevators::EASY) + { + ps->origin[0] = point[0]; + ps->origin[1] = point[1]; + ps->origin[2] = point[2]; + point[2] = (ps->origin[2] - 1.0f) - 0.25f; + + Game::PM_playerTrace(pm, trace, ps->origin, point, &pm->bounds, ps->clientNum, pm->tracemask); + + // If elevators are disabled we need to check that startsolid is false before proceeding + // like later versions of the game do + if (!trace->startsolid || EleSettings >= Elevators::ENABLED) + { + + pml->groundTrace.fraction = trace->fraction; + pml->groundTrace.normal[0] = trace->normal[0]; + pml->groundTrace.normal[1] = trace->normal[1]; + pml->groundTrace.normal[2] = trace->normal[2]; + + pml->groundTrace.surfaceFlags = trace->surfaceFlags; + pml->groundTrace.contents = trace->contents; + pml->groundTrace.hitType = trace->hitType; + pml->groundTrace.hitId = trace->hitId; + + pml->groundTrace.partName = trace->partName; + pml->groundTrace.partGroup = trace->partGroup; + pml->groundTrace.walkable = trace->walkable; + + const auto fraction = trace->fraction; + ps->origin[0] += fraction * (point[0] - ps->origin[0]); + ps->origin[1] += fraction * (point[1] - ps->origin[1]); + ps->origin[2] += fraction * (point[2] - ps->origin[2]); + break; + } + } + + i += 1; + if (i >= 26) + { + ps->groundEntityNum = Game::ENTITYNUM_NONE; + pml->groundPlane = 0; + pml->almostGroundPlane = 0; + pml->walking = 0; + Game::Jump_ClearState(ps); + return 0; + } + } + + return 1; + } + + void Elevators::PM_TraceStub(Game::pmove_s* pm, Game::trace_t* trace, const float* f3, + const float* f4, const Game::Bounds* bounds, int a6, int a7) + { + Game::PM_Trace(pm, trace, f3, f4, bounds, a6, a7); + + if (Elevators::SV_Elevators.get() == Elevators::EASY) + { + trace->allsolid = false; + } + } + + __declspec(naked) void Elevators::PM_CorrectAllSolidStub() { __asm { push eax - mov eax, Elevators::SV_DisableElevators - cmp byte ptr [eax + 16], 1 + pushad + + push [esp + 0x8 + 0x24] + push [esp + 0x8 + 0x24] + push eax + call Elevators::PM_CorrectAllSolid + add esp, 0xC + + mov [esp + 0x20], eax + popad pop eax - // Always skip PM_CorrectAllSolid if SV_DisableElevators is set to 1 - je noElevators - - // Original code - cmp byte ptr [esp + 0x50], 0 - rep movsd - mov esi, [esp + 0x58] - - // Original code flow - push 0x573694 - retn - - noElevators: - - // Original code - rep movsd - mov esi, [esp + 0x58] - - // Jump over call to PM_CorrectAllSolid - push 0x5736AE - retn + ret } } @@ -41,12 +110,20 @@ namespace Components { Dvar::OnInit([] { - Elevators::SV_DisableElevators = Game::Dvar_RegisterBool("sv_disableElevators", - false, Game::DVAR_FLAG_REPLICATED, "Disable elevators"); + static const char* values[] = + { + "Disable elevators", + "Enable elevators", + "Enable easy elevators", + nullptr + }; + + Elevators::SV_Elevators = Game::Dvar_RegisterEnum("sv_Elevators", values, + Elevators::ENABLED, Game::DVAR_FLAG_REPLICATED, "Elevators glitch settings"); }); - // Hook PM_GroundTrace so me way skip PM_CorrectAllSolid (disable elevators) - Utils::Hook(0x573689, Elevators::PM_GroundTraceStub, HOOK_JUMP).install()->quick(); + Utils::Hook(0x57369E, Elevators::PM_CorrectAllSolidStub, HOOK_CALL).install()->quick(); + Utils::Hook(0x570EC5, Elevators::PM_TraceStub, HOOK_CALL).install()->quick(); } Elevators::~Elevators() diff --git a/src/Components/Modules/Elevators.hpp b/src/Components/Modules/Elevators.hpp index 73af502a..ff490a31 100644 --- a/src/Components/Modules/Elevators.hpp +++ b/src/Components/Modules/Elevators.hpp @@ -9,8 +9,11 @@ namespace Components ~Elevators(); private: - static Game::dvar_t* SV_DisableElevators; + enum ElevatorSettings { DISABLED, ENABLED, EASY }; + static Dvar::Var SV_Elevators; - static void PM_GroundTraceStub(); + static int PM_CorrectAllSolid(Game::pmove_s* move, Game::pml_t* pml, Game::trace_t* trace); + static void PM_CorrectAllSolidStub(); + static void PM_TraceStub(Game::pmove_s*, Game::trace_t*, const float*, const float*, const Game::Bounds*, int, int); }; } diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index 86d8b5c5..b09916fe 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -377,6 +377,10 @@ namespace Game Field_AdjustScroll_t Field_AdjustScroll = Field_AdjustScroll_t(0x488C10); AimAssist_ApplyAutoMelee_t AimAssist_ApplyAutoMelee = AimAssist_ApplyAutoMelee_t(0x56A360); + Jump_ClearState_t Jump_ClearState = Jump_ClearState_t(0x04B3890); + PM_playerTrace_t PM_playerTrace = PM_playerTrace_t(0x458980); + PM_Trace_t PM_Trace = PM_Trace_t(0x441F60); + XAssetHeader* DB_XAssetPool = reinterpret_cast(0x7998A8); unsigned int* g_poolSize = reinterpret_cast(0x7995E8); @@ -493,6 +497,8 @@ namespace Game GraphFloat* aaInputGraph = reinterpret_cast(0x7A2FC0); + vec3_t* CorrectSolidDeltas = reinterpret_cast(0x739BB8); // Count 26 + XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize) { int elSize = DB_GetXAssetSizeHandlers[type](); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 7da47215..9e7b7132 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -759,7 +759,7 @@ namespace Game typedef void(__cdecl * SV_Cmd_EndTokenizedString_t)(); extern SV_Cmd_EndTokenizedString_t SV_Cmd_EndTokenizedString; - typedef void(__cdecl* SV_Cmd_ArgvBuffer_t)(int arg, char* buf, int size); + typedef void(__cdecl * SV_Cmd_ArgvBuffer_t)(int arg, char* buf, int size); extern SV_Cmd_ArgvBuffer_t SV_Cmd_ArgvBuffer; typedef void(__cdecl * SV_SetConfigstring_t)(int index, const char* string); @@ -894,6 +894,15 @@ namespace Game typedef void(__cdecl * AimAssist_ApplyAutoMelee_t)(const AimInput* input, AimOutput* output); extern AimAssist_ApplyAutoMelee_t AimAssist_ApplyAutoMelee; + typedef void(__cdecl * Jump_ClearState_t)(playerState_s* ps); + extern Jump_ClearState_t Jump_ClearState; + + typedef void(__cdecl * PM_playerTrace_t)(pmove_s*, trace_t*, const float*, const float*, const Bounds*, int, int); + extern PM_playerTrace_t PM_playerTrace; + + typedef void(__cdecl * PM_Trace_t)(pmove_s*, trace_t*, const float*, const float*, const Bounds*, int, int); + extern PM_Trace_t PM_Trace; + extern XAssetHeader* DB_XAssetPool; extern unsigned int* g_poolSize; @@ -944,6 +953,7 @@ namespace Game extern int* serverMessageSequence; constexpr auto MAX_GENTITIES = 2048u; + constexpr auto ENTITYNUM_NONE = MAX_GENTITIES - 1; extern gentity_t* g_entities; extern netadr_t* connectedHost; @@ -1013,6 +1023,8 @@ namespace Game constexpr auto AIM_ASSIST_GRAPH_COUNT = 4u; extern GraphFloat* aaInputGraph; + extern vec3_t* CorrectSolidDeltas; + XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize); void Menu_FreeItemMemory(Game::itemDef_s* item); void Menu_SetNextCursorItem(Game::UiContext* ctx, Game::menuDef_t* currentMenu, int unk = 1); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 8a74e54d..63acd2a4 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -211,6 +211,31 @@ namespace Game FL_MOVER_SLIDE = 0x8000000 }; + typedef enum + { + HITLOC_NONE, + HITLOC_HELMET, + HITLOC_HEAD, + HITLOC_NECK, + HITLOC_TORSO_UPR, + HITLOC_TORSO_LWR, + HITLOC_R_ARM_UPR, + HITLOC_L_ARM_UPR, + HITLOC_R_ARM_LWR, + HITLOC_L_ARM_LWR, + HITLOC_R_HAND, + HITLOC_L_HAND, + HITLOC_R_LEG_UPR, + HITLOC_L_LEG_UPR, + HITLOC_R_LEG_LWR, + HITLOC_L_LEG_LWR, + HITLOC_R_FOOT, + HITLOC_L_FOOT, + HITLOC_GUN, + HITLOC_SHIELD, + HITLOC_NUM + } hitLocation_t; + struct FxEffectDef; struct pathnode_t; struct pathnode_tree_t; @@ -1132,7 +1157,7 @@ namespace Game int unpredictableEventSequenceOld; int unpredictableEvents[4]; unsigned int unpredictableEventParms[4]; - int clientNum; + int clientNum; // 260 int viewmodelIndex; float viewangles[3]; int viewHeightTarget; @@ -5348,21 +5373,42 @@ namespace Game PLAYER_FLAG_FROZEN = 1 << 2, }; + typedef enum + { + SESS_STATE_PLAYING = 0x0, + SESS_STATE_DEAD = 0x1, + SESS_STATE_SPECTATOR = 0x2, + SESS_STATE_INTERMISSION = 0x3 + } sessionState_t; + + typedef enum + { + CON_DISCONNECTED = 0x0, + CON_CONNECTING = 0x1, + CON_CONNECTED = 0x2 + } clientConnected_t; + typedef struct gclient_s { - unsigned char pad[12764]; - unsigned int team; + playerState_s ps; + sessionState_t sessionState; // 12572 + char pad0[40]; + clientConnected_t connected; // 12616 + char pad1[144]; + unsigned int team; // 12764 char pad2[436]; - int flags; + int flags; // 13204 int spectatorClient; int lastCmdTime; int buttons; - int oldbuttons; - int latched_buttons; - int buttonsSinceLastFrame; - char pad3[700]; + int oldbuttons; // 13220 + int latched_buttons; // 13224 + int buttonsSinceLastFrame; // 13228 + char pad3[700]; // 13232 } gclient_t; + static_assert(sizeof(gclient_t) == 13932); + struct EntHandle { unsigned __int16 number; @@ -6930,15 +6976,43 @@ namespace Game const char* args[9]; }; + enum TraceHitType + { + TRACE_HITTYPE_NONE = 0, + TRACE_HITTYPE_ENTITY = 1, + TRACE_HITTYPE_DYNENT_MODEL = 2, + TRACE_HITTYPE_DYNENT_BRUSH = 3, + TRACE_HITTYPE_GLASS = 4 + }; + +#pragma pack(push, 1) + struct trace_t + { + float fraction; + float normal[3]; + int surfaceFlags; + int contents; + TraceHitType hitType; + unsigned __int16 hitId; + float fractionForHitType; + unsigned __int16 modelIndex; + unsigned __int16 partName; + unsigned __int16 partGroup; + bool allsolid; + bool startsolid; + bool walkable; + }; +#pragma pack(pop) + struct pmove_s { playerState_s* ps; usercmd_s cmd; usercmd_s oldcmd; - int tracemask; + int tracemask; // 84 int numtouch; int touchents[32]; - char __pad0[24]; + Bounds bounds; // 220 float xyspeed; int proneChange; float maxSprintTimeMultiplier; @@ -6952,6 +7026,25 @@ namespace Game unsigned char handler; }; + static_assert(sizeof(pmove_s) == 296); + + struct pml_t + { + float forward[3]; + float right[3]; + float up[3]; + float frametime; + int msec; + int walking; + int groundPlane; + int almostGroundPlane; + trace_t groundTrace; + float impactSpeed; + float previous_origin[3]; + float previous_velocity[3]; + int holdrand; + }; + enum EffectiveStance { PM_EFF_STANCE_DEFAULT = 0, From 8cf5208c29bc46f80322bad4e54ee718b26c015d Mon Sep 17 00:00:00 2001 From: Diavolo Date: Wed, 22 Dec 2021 16:40:30 +0100 Subject: [PATCH 15/27] comments --- src/Components/Modules/Elevators.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/Elevators.cpp b/src/Components/Modules/Elevators.cpp index 1d7aa12d..8195a273 100644 --- a/src/Components/Modules/Elevators.cpp +++ b/src/Components/Modules/Elevators.cpp @@ -36,7 +36,6 @@ namespace Components // like later versions of the game do if (!trace->startsolid || EleSettings >= Elevators::ENABLED) { - pml->groundTrace.fraction = trace->fraction; pml->groundTrace.normal[0] = trace->normal[0]; pml->groundTrace.normal[1] = trace->normal[1]; @@ -79,6 +78,7 @@ namespace Components { Game::PM_Trace(pm, trace, f3, f4, bounds, a6, a7); + // Allow the player to stand even when there is no headroom if (Elevators::SV_Elevators.get() == Elevators::EASY) { trace->allsolid = false; @@ -122,7 +122,10 @@ namespace Components Elevators::ENABLED, Game::DVAR_FLAG_REPLICATED, "Elevators glitch settings"); }); + //Replace PM_CorrectAllSolid Utils::Hook(0x57369E, Elevators::PM_CorrectAllSolidStub, HOOK_CALL).install()->quick(); + + // Place hook in PM_CheckDuck Utils::Hook(0x570EC5, Elevators::PM_TraceStub, HOOK_CALL).install()->quick(); } From ec3cbb4d7c063f7d17376ea967095370b8441cda Mon Sep 17 00:00:00 2001 From: Edo Date: Sun, 26 Dec 2021 17:22:42 +0100 Subject: [PATCH 16/27] Commit suggestion Bad copy paste on my end Co-authored-by: Jan --- src/Components/Modules/Elevators.cpp | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/Components/Modules/Elevators.cpp b/src/Components/Modules/Elevators.cpp index 8195a273..bba565cc 100644 --- a/src/Components/Modules/Elevators.cpp +++ b/src/Components/Modules/Elevators.cpp @@ -36,19 +36,7 @@ namespace Components // like later versions of the game do if (!trace->startsolid || EleSettings >= Elevators::ENABLED) { - pml->groundTrace.fraction = trace->fraction; - pml->groundTrace.normal[0] = trace->normal[0]; - pml->groundTrace.normal[1] = trace->normal[1]; - pml->groundTrace.normal[2] = trace->normal[2]; - - pml->groundTrace.surfaceFlags = trace->surfaceFlags; - pml->groundTrace.contents = trace->contents; - pml->groundTrace.hitType = trace->hitType; - pml->groundTrace.hitId = trace->hitId; - - pml->groundTrace.partName = trace->partName; - pml->groundTrace.partGroup = trace->partGroup; - pml->groundTrace.walkable = trace->walkable; + pml->groundTrace = *trace; const auto fraction = trace->fraction; ps->origin[0] += fraction * (point[0] - ps->origin[0]); From ec43adda5989eb90f41d1c2bf5e90ba0574d4839 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sun, 26 Dec 2021 17:25:13 +0100 Subject: [PATCH 17/27] Address parts of review --- src/Components/Modules/Elevators.cpp | 24 ++++++++++++------------ src/Components/Modules/Elevators.hpp | 4 ++-- src/Game/Structs.hpp | 8 +++++--- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Components/Modules/Elevators.cpp b/src/Components/Modules/Elevators.cpp index 8195a273..fa1df9fd 100644 --- a/src/Components/Modules/Elevators.cpp +++ b/src/Components/Modules/Elevators.cpp @@ -2,7 +2,7 @@ namespace Components { - Dvar::Var Elevators::SV_Elevators; + Dvar::Var Elevators::BG_Elevators; int Elevators::PM_CorrectAllSolid(Game::pmove_s* pm, Game::pml_t* pml, Game::trace_t* trace) { @@ -13,8 +13,8 @@ namespace Components auto* ps = pm->ps; auto i = 0; - const auto EleSettings = Elevators::SV_Elevators.get(); - while (TRUE) + const auto elevatorSetting = Elevators::BG_Elevators.get(); + while (true) { point[0] = ps->origin[0] + Game::CorrectSolidDeltas[i][0]; point[1] = ps->origin[1] + Game::CorrectSolidDeltas[i][1]; @@ -23,7 +23,7 @@ namespace Components Game::PM_playerTrace(pm, trace, point, point, &pm->bounds, ps->clientNum, pm->tracemask); // If the player wishes to glitch without effort they can do so - if (!trace->startsolid || EleSettings == Elevators::EASY) + if (!trace->startsolid || elevatorSetting == Elevators::EASY) { ps->origin[0] = point[0]; ps->origin[1] = point[1]; @@ -34,7 +34,7 @@ namespace Components // If elevators are disabled we need to check that startsolid is false before proceeding // like later versions of the game do - if (!trace->startsolid || EleSettings >= Elevators::ENABLED) + if (!trace->startsolid || elevatorSetting >= Elevators::ENABLED) { pml->groundTrace.fraction = trace->fraction; pml->groundTrace.normal[0] = trace->normal[0]; @@ -73,13 +73,13 @@ namespace Components return 1; } - void Elevators::PM_TraceStub(Game::pmove_s* pm, Game::trace_t* trace, const float* f3, + void Elevators::PM_Trace_Hk(Game::pmove_s* pm, Game::trace_t* trace, const float* f3, const float* f4, const Game::Bounds* bounds, int a6, int a7) { Game::PM_Trace(pm, trace, f3, f4, bounds, a6, a7); // Allow the player to stand even when there is no headroom - if (Elevators::SV_Elevators.get() == Elevators::EASY) + if (Elevators::BG_Elevators.get() == Elevators::EASY) { trace->allsolid = false; } @@ -112,13 +112,13 @@ namespace Components { static const char* values[] = { - "Disable elevators", - "Enable elevators", - "Enable easy elevators", + "off", + "normal", + "easy", nullptr }; - Elevators::SV_Elevators = Game::Dvar_RegisterEnum("sv_Elevators", values, + Elevators::BG_Elevators = Game::Dvar_RegisterEnum("bg_elevators", values, Elevators::ENABLED, Game::DVAR_FLAG_REPLICATED, "Elevators glitch settings"); }); @@ -126,7 +126,7 @@ namespace Components Utils::Hook(0x57369E, Elevators::PM_CorrectAllSolidStub, HOOK_CALL).install()->quick(); // Place hook in PM_CheckDuck - Utils::Hook(0x570EC5, Elevators::PM_TraceStub, HOOK_CALL).install()->quick(); + Utils::Hook(0x570EC5, Elevators::PM_Trace_Hk, HOOK_CALL).install()->quick(); } Elevators::~Elevators() diff --git a/src/Components/Modules/Elevators.hpp b/src/Components/Modules/Elevators.hpp index ff490a31..02b0b64e 100644 --- a/src/Components/Modules/Elevators.hpp +++ b/src/Components/Modules/Elevators.hpp @@ -10,10 +10,10 @@ namespace Components private: enum ElevatorSettings { DISABLED, ENABLED, EASY }; - static Dvar::Var SV_Elevators; + static Dvar::Var BG_Elevators; static int PM_CorrectAllSolid(Game::pmove_s* move, Game::pml_t* pml, Game::trace_t* trace); static void PM_CorrectAllSolidStub(); - static void PM_TraceStub(Game::pmove_s*, Game::trace_t*, const float*, const float*, const Game::Bounds*, int, int); + static void PM_Trace_Hk(Game::pmove_s*, Game::trace_t*, const float*, const float*, const Game::Bounds*, int, int); }; } diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 63acd2a4..bad13871 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -6985,16 +6985,15 @@ namespace Game TRACE_HITTYPE_GLASS = 4 }; -#pragma pack(push, 1) struct trace_t { float fraction; float normal[3]; int surfaceFlags; int contents; + const char* material; TraceHitType hitType; unsigned __int16 hitId; - float fractionForHitType; unsigned __int16 modelIndex; unsigned __int16 partName; unsigned __int16 partGroup; @@ -7002,7 +7001,8 @@ namespace Game bool startsolid; bool walkable; }; -#pragma pack(pop) + + static_assert(sizeof(trace_t) == 0x2C); struct pmove_s { @@ -7045,6 +7045,8 @@ namespace Game int holdrand; }; + static_assert(sizeof(pml_t) == 0x84); + enum EffectiveStance { PM_EFF_STANCE_DEFAULT = 0, From 41874769aa3a57337a0d58ac779592302c207d0a Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sun, 26 Dec 2021 17:33:48 +0100 Subject: [PATCH 18/27] Address review --- src/Components/Modules/Elevators.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Components/Modules/Elevators.cpp b/src/Components/Modules/Elevators.cpp index 224aba21..f9f42354 100644 --- a/src/Components/Modules/Elevators.cpp +++ b/src/Components/Modules/Elevators.cpp @@ -36,12 +36,6 @@ namespace Components // like later versions of the game do if (!trace->startsolid || elevatorSetting >= Elevators::ENABLED) { - pml->groundTrace = *trace; - - const auto fraction = trace->fraction; - ps->origin[0] += fraction * (point[0] - ps->origin[0]); - ps->origin[1] += fraction * (point[1] - ps->origin[1]); - ps->origin[2] += fraction * (point[2] - ps->origin[2]); break; } } @@ -58,6 +52,13 @@ namespace Components } } + pml->groundTrace = *trace; + + const auto fraction = trace->fraction; + ps->origin[0] += fraction * (point[0] - ps->origin[0]); + ps->origin[1] += fraction * (point[1] - ps->origin[1]); + ps->origin[2] += fraction * (point[2] - ps->origin[2]); + return 1; } From eb88d587fc05f46963930b3c152054009bf4d7cf Mon Sep 17 00:00:00 2001 From: Diavolo Date: Tue, 28 Dec 2021 10:07:39 +0100 Subject: [PATCH 19/27] Added prone elevators --- src/Components/Modules/Elevators.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/Elevators.cpp b/src/Components/Modules/Elevators.cpp index f9f42354..9d96a01b 100644 --- a/src/Components/Modules/Elevators.cpp +++ b/src/Components/Modules/Elevators.cpp @@ -114,8 +114,10 @@ namespace Components //Replace PM_CorrectAllSolid Utils::Hook(0x57369E, Elevators::PM_CorrectAllSolidStub, HOOK_CALL).install()->quick(); - // Place hook in PM_CheckDuck + // Place hooks in PM_CheckDuck. If the elevators dvar is set to easy the + // flags for duck/prone will always be removed from the player state Utils::Hook(0x570EC5, Elevators::PM_Trace_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x570E0B, Elevators::PM_Trace_Hk, HOOK_CALL).install()->quick(); } Elevators::~Elevators() From 3e7b57eb07f10d2a9f01805799de3008753b01fc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 3 Jan 2022 17:19:08 +0000 Subject: [PATCH 20/27] Bump deps/zlib from `c3f3043` to `2014a99` Bumps [deps/zlib](https://github.com/madler/zlib) from `c3f3043` to `2014a99`. - [Release notes](https://github.com/madler/zlib/releases) - [Commits](https://github.com/madler/zlib/compare/c3f3043f7aa80750245f8166a338c4877020b589...2014a993addbc8f1b9785d97f55fd189792c2f78) --- updated-dependencies: - dependency-name: deps/zlib dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- deps/zlib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/zlib b/deps/zlib index c3f3043f..2014a993 160000 --- a/deps/zlib +++ b/deps/zlib @@ -1 +1 @@ -Subproject commit c3f3043f7aa80750245f8166a338c4877020b589 +Subproject commit 2014a993addbc8f1b9785d97f55fd189792c2f78 From 22076ef0960eb35b6ccef5c0c3cec79c4c92c539 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Fri, 7 Jan 2022 12:33:53 +0100 Subject: [PATCH 21/27] Add prone to crouch ele --- src/Components/Modules/Elevators.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Components/Modules/Elevators.cpp b/src/Components/Modules/Elevators.cpp index 9d96a01b..42b80108 100644 --- a/src/Components/Modules/Elevators.cpp +++ b/src/Components/Modules/Elevators.cpp @@ -118,6 +118,7 @@ namespace Components // flags for duck/prone will always be removed from the player state Utils::Hook(0x570EC5, Elevators::PM_Trace_Hk, HOOK_CALL).install()->quick(); Utils::Hook(0x570E0B, Elevators::PM_Trace_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x570D70, Elevators::PM_Trace_Hk, HOOK_CALL).install()->quick(); } Elevators::~Elevators() From 0fe6a80429bbc543536805675902a4bf21df016d Mon Sep 17 00:00:00 2001 From: FutureRave Date: Mon, 10 Jan 2022 15:28:37 +0000 Subject: [PATCH 22/27] Refactor startup messages --- src/Components/Modules/StartupMessages.cpp | 10 ++++++++-- src/Components/Modules/StartupMessages.hpp | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/StartupMessages.cpp b/src/Components/Modules/StartupMessages.cpp index 55733f9a..b7be14b3 100644 --- a/src/Components/Modules/StartupMessages.cpp +++ b/src/Components/Modules/StartupMessages.cpp @@ -23,16 +23,22 @@ namespace Components StartupMessages::TotalMessages = StartupMessages::MessageList.size(); } - std::string message = StartupMessages::MessageList.front(); - StartupMessages::MessageList.pop_front(); + const auto& message = StartupMessages::MessageList.front(); Game::Dvar_SetStringByName("ui_startupMessage", message.data()); Game::Dvar_SetStringByName("ui_startupMessageTitle", Utils::String::VA("Messages (%d/%d)", StartupMessages::TotalMessages - StartupMessages::MessageList.size(), StartupMessages::TotalMessages)); Game::Dvar_SetStringByName("ui_startupNextButtonText", StartupMessages::MessageList.size() ? "Next" : "Close"); Game::Cbuf_AddText(0, "openmenu startup_messages"); + + StartupMessages::MessageList.pop_front(); }); } + StartupMessages::~StartupMessages() + { + StartupMessages::MessageList.clear(); + } + void StartupMessages::AddMessage(const std::string& message) { StartupMessages::MessageList.push_back(message); diff --git a/src/Components/Modules/StartupMessages.hpp b/src/Components/Modules/StartupMessages.hpp index 7680c77a..6ecf3cbe 100644 --- a/src/Components/Modules/StartupMessages.hpp +++ b/src/Components/Modules/StartupMessages.hpp @@ -6,6 +6,7 @@ namespace Components { public: StartupMessages(); + ~StartupMessages(); static void AddMessage(const std::string& message); From ca3c0a7b2fbcb58f137b593984ba392db7e235a9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 11 Jan 2022 17:15:50 +0000 Subject: [PATCH 23/27] Bump deps/pdcurses from `f1cd4f4` to `6c8e32e` Bumps [deps/pdcurses](https://github.com/wmcbrine/PDCurses) from `f1cd4f4` to `6c8e32e`. - [Release notes](https://github.com/wmcbrine/PDCurses/releases) - [Commits](https://github.com/wmcbrine/PDCurses/compare/f1cd4f4569451a5028ddf3d3c202f0ad6b1ae446...6c8e32ee9b8fcdcb5765b81cbfce22827957dbe8) --- updated-dependencies: - dependency-name: deps/pdcurses dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- deps/pdcurses | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/pdcurses b/deps/pdcurses index f1cd4f45..6c8e32ee 160000 --- a/deps/pdcurses +++ b/deps/pdcurses @@ -1 +1 @@ -Subproject commit f1cd4f4569451a5028ddf3d3c202f0ad6b1ae446 +Subproject commit 6c8e32ee9b8fcdcb5765b81cbfce22827957dbe8 From 2785343eace663b155c858fe7e2d233d828ef162 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Wed, 12 Jan 2022 17:48:20 +0000 Subject: [PATCH 24/27] document player state flags in gamepad module --- src/Components/Modules/Gamepad.cpp | 5 ++--- src/Game/Structs.hpp | 34 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Gamepad.cpp b/src/Components/Modules/Gamepad.cpp index c3555fc7..e85e61dd 100644 --- a/src/Components/Modules/Gamepad.cpp +++ b/src/Components/Modules/Gamepad.cpp @@ -536,14 +536,13 @@ namespace Components if (weaponDef->requireLockonToFire) return false; - if (ps->linkFlags & 4) + if (ps->linkFlags & Game::PLF_WEAPONVIEW_ONLY) return false; if (ps->weaponState >= Game::WEAPON_STUNNED_START && ps->weaponState <= Game::WEAPON_STUNNED_END) return false; - // The game checks for these flags. Their meaning is to be researched if necessary. - if (ps->eFlags & 0x100C00) + if (ps->eFlags & (Game::EF_VEHICLE_ACTIVE | Game::EF_TURRET_ACTIVE_DUCK | Game::EF_TURRET_ACTIVE_PRONE)) return false; if (!ps->hasAmmo) diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index bad13871..13995f34 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1110,6 +1110,40 @@ namespace Game PM_DEAD_LINKED = 0x9, }; + enum playerEFlag + { + EF_NONSOLID_BMODEL = 0x1, + EF_TELEPORT_BIT = 0x2, + EF_CROUCHING = 0x4, + EF_PRONE = 0x8, + EF_NODRAW = 0x20, + EF_TIMED_OBJECT = 0x40, + EF_VOTED = 0x80, + EF_TALK = 0x100, + EF_FIRING = 0x200, + EF_TURRET_ACTIVE_PRONE = 0x400, + EF_TURRET_ACTIVE_DUCK = 0x800, + EF_LOCK_LIGHT_VIS = 0x1000, + EF_AIM_ASSIST = 0x2000, + EF_LOOP_RUMBLE = 0x4000, + EF_LASER_SIGHT = 0x8000, + EF_MANTLE = 0x10000, + EF_DEAD = 0x20000, + EF_ADS = 0x40000, + EF_NEW = 0x80000, + EF_VEHICLE_ACTIVE = 0x100000, + EF_JAMMING = 0x200000, + EF_COMPASS_PING = 0x400000, + EF_SOFT = 0x800000 + }; + + enum playerLinkFlag + { + PLF_ANGLES_LOCKED = 0x1, + PLF_USES_OFFSET = 0x2, + PLF_WEAPONVIEW_ONLY = 0x4 + }; + struct playerState_s { int commandTime; From 9bb3ae64e3d5a25555abfeb5330e524ce11e3aca Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 13 Jan 2022 17:23:21 +0000 Subject: [PATCH 25/27] Bump deps/pdcurses from `6c8e32e` to `af0fd7d` Bumps [deps/pdcurses](https://github.com/wmcbrine/PDCurses) from `6c8e32e` to `af0fd7d`. - [Release notes](https://github.com/wmcbrine/PDCurses/releases) - [Commits](https://github.com/wmcbrine/PDCurses/compare/6c8e32ee9b8fcdcb5765b81cbfce22827957dbe8...af0fd7dba8d179cc71acd26b999e6efdd1f93011) --- updated-dependencies: - dependency-name: deps/pdcurses dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- deps/pdcurses | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/pdcurses b/deps/pdcurses index 6c8e32ee..af0fd7db 160000 --- a/deps/pdcurses +++ b/deps/pdcurses @@ -1 +1 @@ -Subproject commit 6c8e32ee9b8fcdcb5765b81cbfce22827957dbe8 +Subproject commit af0fd7dba8d179cc71acd26b999e6efdd1f93011 From 64dc2564750ab34066cb3b4327241cec78856f45 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 17 Jan 2022 17:16:36 +0000 Subject: [PATCH 26/27] Bump deps/pdcurses from `af0fd7d` to `2fa0f10` Bumps [deps/pdcurses](https://github.com/wmcbrine/PDCurses) from `af0fd7d` to `2fa0f10`. - [Release notes](https://github.com/wmcbrine/PDCurses/releases) - [Commits](https://github.com/wmcbrine/PDCurses/compare/af0fd7dba8d179cc71acd26b999e6efdd1f93011...2fa0f10dd844da47ee83c05a40a1ec541ceb95e1) --- updated-dependencies: - dependency-name: deps/pdcurses dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- deps/pdcurses | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/pdcurses b/deps/pdcurses index af0fd7db..2fa0f10d 160000 --- a/deps/pdcurses +++ b/deps/pdcurses @@ -1 +1 @@ -Subproject commit af0fd7dba8d179cc71acd26b999e6efdd1f93011 +Subproject commit 2fa0f10dd844da47ee83c05a40a1ec541ceb95e1 From c0670ba0a314a0fa3e0c26cc4182565478e02827 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Mon, 17 Jan 2022 17:21:51 +0000 Subject: [PATCH 27/27] Revert some changes and use const char pointer where it's appropriate --- src/Components/Modules/Command.cpp | 10 +++++----- src/Components/Modules/Command.hpp | 8 ++++---- src/Components/Modules/Dvar.cpp | 24 ++++++++++++------------ src/Components/Modules/Friends.cpp | 2 +- src/Components/Modules/Playlist.cpp | 2 +- src/Components/Modules/UIScript.cpp | 9 ++------- src/Components/Modules/ZoneBuilder.cpp | 4 ++-- src/Game/Functions.cpp | 8 ++++---- 8 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/Components/Modules/Command.cpp b/src/Components/Modules/Command.cpp index 52a1b3fc..92962240 100644 --- a/src/Components/Modules/Command.cpp +++ b/src/Components/Modules/Command.cpp @@ -18,14 +18,14 @@ namespace Components return result; } - char* Command::Params::operator[](size_t index) + const char* Command::Params::operator[](size_t index) { return this->get(index); } - char* Command::ClientParams::get(size_t index) + const char* Command::ClientParams::get(size_t index) { - if (index >= this->length()) return const_cast(""); + if (index >= this->length()) return ""; return Game::cmd_argv[this->commandId][index]; } @@ -34,9 +34,9 @@ namespace Components return Game::cmd_argc[this->commandId]; } - char* Command::ServerParams::get(size_t index) + const char* Command::ServerParams::get(size_t index) { - if (index >= this->length()) return const_cast(""); + if (index >= this->length()) return ""; return Game::cmd_argv_sv[this->commandId][index]; } diff --git a/src/Components/Modules/Command.hpp b/src/Components/Modules/Command.hpp index 17f90d7c..4e07ba87 100644 --- a/src/Components/Modules/Command.hpp +++ b/src/Components/Modules/Command.hpp @@ -10,11 +10,11 @@ namespace Components public: Params() {}; virtual ~Params() {}; - virtual char* get(size_t index) = 0; + virtual const char* get(size_t index) = 0; virtual size_t length() = 0; virtual std::string join(size_t startIndex); - virtual char* operator[](size_t index); + virtual const char* operator[](size_t index); }; class ClientParams : public Params @@ -24,7 +24,7 @@ namespace Components ClientParams(const ClientParams &obj) : commandId(obj.commandId) {}; ClientParams() : ClientParams(*Game::cmd_id) {}; - char* get(size_t index) override; + const char* get(size_t index) override; size_t length() override; private: @@ -38,7 +38,7 @@ namespace Components ServerParams(const ServerParams &obj) : commandId(obj.commandId) {}; ServerParams() : ServerParams(*Game::cmd_id_sv) {}; - char* get(size_t index) override; + const char* get(size_t index) override; size_t length() override; private: diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index d0892d2e..52ba6fed 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -21,19 +21,19 @@ namespace Components return this->dvar; } - template <> char* Dvar::Var::get() - { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_STRING && this->dvar->current.string) - { - return const_cast(this->dvar->current.string); - } - - return const_cast(""); - } - template <> const char* Dvar::Var::get() { - return this->get(); + if (this->dvar == nullptr) + return ""; + + if (this->dvar->type == Game::dvar_type::DVAR_TYPE_STRING + || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) + { + if (this->dvar->current.string != nullptr) + return this->dvar->current.string; + } + + return ""; } template <> int Dvar::Var::get() @@ -198,7 +198,7 @@ namespace Components Scheduler::OnFrame([]() { static std::string lastValidName = "Unknown Soldier"; - std::string name = Dvar::Var("name").get(); + std::string name = Dvar::Var("name").get(); // Don't perform any checks if name didn't change if (name == lastValidName) return; diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index 5228537b..13b9854e 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -186,7 +186,7 @@ namespace Components { std::lock_guard _(Friends::Mutex); - const unsigned int modId = *reinterpret_cast(const_cast("IW4x")) | 0x80000000; + const unsigned int modId = *reinterpret_cast("IW4x") | 0x80000000; // Split up the list for (auto entry : Friends::FriendsList) diff --git a/src/Components/Modules/Playlist.cpp b/src/Components/Modules/Playlist.cpp index 7088dbf3..c90183cc 100644 --- a/src/Components/Modules/Playlist.cpp +++ b/src/Components/Modules/Playlist.cpp @@ -21,7 +21,7 @@ namespace Components Dvar::Var("xblive_privateserver").set(false); - std::string playlistFilename = Dvar::Var("playlistFilename").get(); + std::string playlistFilename = Dvar::Var("playlistFilename").get(); FileSystem::File playlist(playlistFilename); if (playlist.exists()) diff --git a/src/Components/Modules/UIScript.cpp b/src/Components/Modules/UIScript.cpp index 1ecd6d4c..bc07bd7f 100644 --- a/src/Components/Modules/UIScript.cpp +++ b/src/Components/Modules/UIScript.cpp @@ -15,19 +15,14 @@ namespace Components return 0; } - template<> char* UIScript::Token::get() + template<> const char* UIScript::Token::get() { if (this->isValid()) { return this->token; } - return const_cast(""); - } - - template<> const char* UIScript::Token::get() - { - return this->get(); + return ""; } template<> std::string UIScript::Token::get() diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index f8f45fc4..a9a38a33 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -1300,7 +1300,7 @@ namespace Components }, nullptr, false); // HACK: set language to 'techsets' to load from that dir - char* language = Utils::Hook::Get(0x649E740); + const char* language = Utils::Hook::Get(0x649E740); Utils::Hook::Set(0x649E740, "techsets"); // load generated techset fastfiles @@ -1447,7 +1447,7 @@ namespace Components Utils::IO::WriteFile("zone_source/techsets/techsets.csv", csvStr.data()); // set language back - Utils::Hook::Set(0x649E740, language); + Utils::Hook::Set(0x649E740, language); Logger::Print("Building zone 'techsets/techsets'...\n"); Zone("techsets/techsets").build(); diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index d66a4154..c52d6f5f 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -773,9 +773,9 @@ namespace Game float Vec2Normalize(vec2_t& vec) { - const float length = std::sqrt((vec[0] * vec[0]) + (vec[1] * vec[1])); + const auto length = std::sqrt(vec[0] * vec[0] + vec[1] * vec[1]); - if(length > 0.0f) + if (length > 0.0f) { vec[0] /= length; vec[1] /= length; @@ -786,9 +786,9 @@ namespace Game float Vec3Normalize(vec3_t& vec) { - const float length = std::sqrt(std::pow(vec[0], 2.0f) + std::pow(vec[1], 2.0f) + std::pow(vec[2], 2.0f)); + const auto length = std::sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); - if(length > 0.0f) + if (length > 0.0f) { vec[0] /= length; vec[1] /= length;