From 4a200e0c40c3d1aee84d9bb45e1b1ff5222b4754 Mon Sep 17 00:00:00 2001 From: TotallyNotElite <1yourexperiment@protonmail.com> Date: Tue, 3 Jul 2018 21:46:52 +0200 Subject: [PATCH] Refactoring --- include/helpers.hpp | 2 +- src/hack.cpp | 4 -- src/hacks/FollowBot.cpp | 22 ++---- src/helpers.cpp | 151 +++++++++++++++++++++++++++------------- 4 files changed, 109 insertions(+), 70 deletions(-) diff --git a/include/helpers.hpp b/include/helpers.hpp index 24ebddbd..82aa9d55 100755 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -81,7 +81,7 @@ Vector VischeckWall(CachedEntity *player, CachedEntity *target, float maxdist, bool checkWalkable); float vectorMax(Vector i); Vector vectorAbs(Vector i); -bool canReachVector(Vector loc); +bool canReachVector(Vector loc, Vector dest = {0,0,0}); bool isJumping(Vector vec); bool LineIntersectsBox(Vector &bmin, Vector &bmax, Vector &lmin, Vector &lmax); diff --git a/src/hack.cpp b/src/hack.cpp index 1d203214..07e04634 100644 --- a/src/hack.cpp +++ b/src/hack.cpp @@ -491,10 +491,6 @@ free(logname);*/ #if ENABLE_VISUALS InitStrings(); -#if ENABLE_GUI - // cat_reloadscheme to load imgui - hack::command_stack().push("cat_reloadscheme"); -#endif #ifndef FEATURE_EFFECTS_DISABLED if (g_ppScreenSpaceRegistrationHead && g_pScreenSpaceEffects) { diff --git a/src/hacks/FollowBot.cpp b/src/hacks/FollowBot.cpp index a28dce72..57d93b23 100644 --- a/src/hacks/FollowBot.cpp +++ b/src/hacks/FollowBot.cpp @@ -102,13 +102,7 @@ bool addCrumbs(CachedEntity *target, Vector corner = g_pLocalPlayer->v_Origin) int maxiterations = floor(corner.DistTo(g_pLocalPlayer->v_Origin)) / 40; for (int i = 0; i < maxiterations; i++) { - Vector result = g_pLocalPlayer->v_Origin + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1); - if (!canReachVector(result)) - { - breadcrumbs.clear(); - return false; - } - breadcrumbs.push_back(result); + breadcrumbs.push_back(g_pLocalPlayer->v_Origin + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1)); } } @@ -116,13 +110,7 @@ bool addCrumbs(CachedEntity *target, Vector corner = g_pLocalPlayer->v_Origin) int maxiterations = floor(corner.DistTo(target->m_vecOrigin())) / 40; for (int i = 0; i < maxiterations; i++) { - Vector result = corner + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1); - if (!canReachVector(result)) - { - breadcrumbs.clear(); - return false; - } - breadcrumbs.push_back(result); + breadcrumbs.push_back(corner + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1)); } return true; } @@ -245,8 +233,6 @@ void WorldTick() Vector indirectOrigin = VischeckWall(LOCAL_E, entity, 250, true); //get the corner location that the future target is visible from if (!indirectOrigin.z) //if we couldn't find it, exit continue; - //breadcrumbs.clear(); //we need to ensure that the breadcrumbs std::vector is empty - //breadcrumbs.push_back(indirectOrigin); //add the corner location to the breadcrumb list if (!addCrumbs(entity, indirectOrigin)) continue; } @@ -255,9 +241,9 @@ void WorldTick() if (!VisCheckEntFromEnt(LOCAL_E, entity)) continue; } + // favor closer entitys if (follow_target && - ENTITY(follow_target)->m_flDistance() > - entity->m_flDistance()) // favor closer entitys + ENTITY(follow_target)->m_flDistance() > entity->m_flDistance()) continue; // ooooo, a target follow_target = i; diff --git a/src/helpers.cpp b/src/helpers.cpp index daefa326..d7153cec 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -116,8 +116,13 @@ Vector VischeckWall(CachedEntity *player, CachedEntity *target, float maxdist, // if we can see an entity, we don't need to run calculations if (VisCheckEntFromEnt(player, target)) + { + if (!checkWalkable) + return origin; + else if (canReachVector(origin, target->m_vecOrigin())) + return origin; + } - return origin; for (int i = 0; i < 4; i++) // for loop for all 4 directions { // 40 * maxiterations = range in HU @@ -148,8 +153,11 @@ Vector VischeckWall(CachedEntity *player, CachedEntity *target, float maxdist, continue; if (!checkWalkable) return virtualOrigin; + // check if the location is accessible - if (canReachVector(virtualOrigin)) + if (!canReachVector(origin, virtualOrigin)) + continue; + if (canReachVector(virtualOrigin, target->m_vecOrigin())) return virtualOrigin; } } @@ -160,8 +168,7 @@ Vector VischeckWall(CachedEntity *player, CachedEntity *target, float maxdist, // Returns a vectors max value. For example: {123,-150, 125} = 125 float vectorMax(Vector i) { - float res = fmaxf(i.x, i.y); - return fmaxf(res, i.z); + return fmaxf(fmaxf(i.x, i.y), i.z); } // Returns a vectors absolute value. For example {123,-150, 125} = {123,150, @@ -176,49 +183,99 @@ Vector vectorAbs(Vector i) } // check to see if we can reach a vector or if it is too high / doesn't leave -// enough space for the player -bool canReachVector(Vector loc) +// enough space for the player, optional second vector +bool canReachVector(Vector loc, Vector dest) { - // check if the vector is too high above ground - trace_t trace; - Ray_t ray; - Vector down = loc; - down.z = down.z - 50; - ray.Init(loc, down); - g_ITrace->TraceRay(ray, 0x4200400B, &trace::filter_no_player, &trace); - // higher to avoid small false positives, player can jump 42 hu according to - // the tf2 wiki - if (!(trace.startpos.DistTo(trace.endpos) <= 45)) - return false; - - // check if there is enough space arround the vector for a player to fit - // for loop for all 4 directions - for (int i = 0; i < 4; i++) + if (!dest.IsZero()) { - Vector directionalLoc = loc; - // what direction to check - switch (i) + Vector dist = dest - loc; + int maxiterations = floor(dest.DistTo(loc)) / 40; + for (int i = 0; i < maxiterations; i++) { - case 0: - directionalLoc.x = directionalLoc.x + 40; - break; - case 1: - directionalLoc.x = directionalLoc.x - 40; - break; - case 2: - directionalLoc.y = directionalLoc.y + 40; - break; - case 3: - directionalLoc.y = directionalLoc.y - 40; - break; + Vector vec = loc + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1); + + trace_t trace; + Ray_t ray; + Vector down = vec; + down.z = down.z - 50; + ray.Init(vec, down); + g_ITrace->TraceRay(ray, 0x4200400B, &trace::filter_no_player, + &trace); + if (!(trace.startpos.DistTo(trace.endpos) <= 45)) + return false; + + for (int j = 0; j < 4; j++) + { + Vector directionalLoc = vec; + // what direction to check + switch (j) + { + case 0: + directionalLoc.x = directionalLoc.x + 40; + break; + case 1: + directionalLoc.x = directionalLoc.x - 40; + break; + case 2: + directionalLoc.y = directionalLoc.y + 40; + break; + case 3: + directionalLoc.y = directionalLoc.y - 40; + break; + } + trace_t trace2; + Ray_t ray2; + ray2.Init(vec, directionalLoc); + g_ITrace->TraceRay(ray2, 0x4200400B, &trace::filter_no_player, + &trace2); + // distance of trace < than 26 + if (trace2.startpos.DistTo(trace2.endpos) < 26.0f) + return false; + } } - trace_t trace2; - Ray_t ray2; - ray2.Init(loc, directionalLoc); - g_ITrace->TraceRay(ray2, 0x4200400B, &trace::filter_no_player, &trace); - // distance of trace < than 26 - if (trace2.startpos.DistTo(trace2.endpos) < 26.0f) + } + else + { + // check if the vector is too high above ground + trace_t trace; + Ray_t ray; + Vector down = loc; + down.z = down.z - 50; + ray.Init(loc, down); + g_ITrace->TraceRay(ray, 0x4200400B, &trace::filter_no_player, &trace); + // higher to avoid small false positives, player can jump 42 hu according to + // the tf2 wiki + if (!(trace.startpos.DistTo(trace.endpos) <= 45)) return false; + // check if there is enough space arround the vector for a player to fit + // for loop for all 4 directions + for (int i = 0; i < 4; i++) + { + Vector directionalLoc = loc; + // what direction to check + switch (i) + { + case 0: + directionalLoc.x = directionalLoc.x + 40; + break; + case 1: + directionalLoc.x = directionalLoc.x - 40; + break; + case 2: + directionalLoc.y = directionalLoc.y + 40; + break; + case 3: + directionalLoc.y = directionalLoc.y - 40; + break; + } + trace_t trace2; + Ray_t ray2; + ray2.Init(loc, directionalLoc); + g_ITrace->TraceRay(ray, 0x4200400B, &trace::filter_no_player, &trace2); + // distance of trace < than 26 + if (trace2.startpos.DistTo(trace2.endpos) < 26.0f) + return false; + } } return true; } @@ -230,15 +287,15 @@ bool isJumping(Vector vec) trace_t trace; Ray_t ray; Vector down = vec; - Vector loc = vec; + Vector loc = vec; down.z = down.z - 50; - loc.z = loc.z + 5; + loc.z = loc.z + 5; ray.Init(vec, down); - //trace::filter_no_player.SetSelf(RAW_ENT(g_pLocalPlayer->entity)); + // trace::filter_no_player.SetSelf(RAW_ENT(g_pLocalPlayer->entity)); g_ITrace->TraceRay(ray, 0x4200400B, &trace::filter_no_player, &trace); // lower to avoid small false negatives, player can jump 42 hu according to // the tf2 wiki, higher because loc.z = loc.z + 5; - if (fabsf(trace.startpos.DistTo(trace.endpos)) > 45) + if (trace.startpos.DistTo(trace.endpos) > 45) return true; return false; } @@ -1173,7 +1230,7 @@ void PrintChat(const char *fmt, ...) CHudBaseChat *chat = (CHudBaseChat *) g_CHUD->FindElement("CHudChat"); if (chat) { - std::unique_ptr buf(new char[1024]); + std::unique_ptr buf(new char[1024]); va_list list; va_start(list, fmt); vsprintf(buf.get(), fmt, list);