Refactoring

This commit is contained in:
TotallyNotElite 2018-07-03 21:46:52 +02:00
parent 0c0c40a454
commit 4a200e0c40
4 changed files with 109 additions and 70 deletions

View File

@ -81,7 +81,7 @@ Vector VischeckWall(CachedEntity *player, CachedEntity *target, float maxdist,
bool checkWalkable); bool checkWalkable);
float vectorMax(Vector i); float vectorMax(Vector i);
Vector vectorAbs(Vector i); Vector vectorAbs(Vector i);
bool canReachVector(Vector loc); bool canReachVector(Vector loc, Vector dest = {0,0,0});
bool isJumping(Vector vec); bool isJumping(Vector vec);
bool LineIntersectsBox(Vector &bmin, Vector &bmax, Vector &lmin, Vector &lmax); bool LineIntersectsBox(Vector &bmin, Vector &bmax, Vector &lmin, Vector &lmax);

View File

@ -491,10 +491,6 @@ free(logname);*/
#if ENABLE_VISUALS #if ENABLE_VISUALS
InitStrings(); InitStrings();
#if ENABLE_GUI
// cat_reloadscheme to load imgui
hack::command_stack().push("cat_reloadscheme");
#endif
#ifndef FEATURE_EFFECTS_DISABLED #ifndef FEATURE_EFFECTS_DISABLED
if (g_ppScreenSpaceRegistrationHead && g_pScreenSpaceEffects) if (g_ppScreenSpaceRegistrationHead && g_pScreenSpaceEffects)
{ {

View File

@ -102,13 +102,7 @@ bool addCrumbs(CachedEntity *target, Vector corner = g_pLocalPlayer->v_Origin)
int maxiterations = floor(corner.DistTo(g_pLocalPlayer->v_Origin)) / 40; int maxiterations = floor(corner.DistTo(g_pLocalPlayer->v_Origin)) / 40;
for (int i = 0; i < maxiterations; i++) for (int i = 0; i < maxiterations; i++)
{ {
Vector result = g_pLocalPlayer->v_Origin + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1); breadcrumbs.push_back(g_pLocalPlayer->v_Origin + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1));
if (!canReachVector(result))
{
breadcrumbs.clear();
return false;
}
breadcrumbs.push_back(result);
} }
} }
@ -116,13 +110,7 @@ bool addCrumbs(CachedEntity *target, Vector corner = g_pLocalPlayer->v_Origin)
int maxiterations = floor(corner.DistTo(target->m_vecOrigin())) / 40; int maxiterations = floor(corner.DistTo(target->m_vecOrigin())) / 40;
for (int i = 0; i < maxiterations; i++) for (int i = 0; i < maxiterations; i++)
{ {
Vector result = corner + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1); breadcrumbs.push_back(corner + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1));
if (!canReachVector(result))
{
breadcrumbs.clear();
return false;
}
breadcrumbs.push_back(result);
} }
return true; 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 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 if (!indirectOrigin.z) //if we couldn't find it, exit
continue; 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)) if (!addCrumbs(entity, indirectOrigin))
continue; continue;
} }
@ -255,9 +241,9 @@ void WorldTick()
if (!VisCheckEntFromEnt(LOCAL_E, entity)) if (!VisCheckEntFromEnt(LOCAL_E, entity))
continue; continue;
} }
// favor closer entitys
if (follow_target && if (follow_target &&
ENTITY(follow_target)->m_flDistance() > ENTITY(follow_target)->m_flDistance() > entity->m_flDistance())
entity->m_flDistance()) // favor closer entitys
continue; continue;
// ooooo, a target // ooooo, a target
follow_target = i; follow_target = i;

View File

@ -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 we can see an entity, we don't need to run calculations
if (VisCheckEntFromEnt(player, target)) 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 for (int i = 0; i < 4; i++) // for loop for all 4 directions
{ {
// 40 * maxiterations = range in HU // 40 * maxiterations = range in HU
@ -148,8 +153,11 @@ Vector VischeckWall(CachedEntity *player, CachedEntity *target, float maxdist,
continue; continue;
if (!checkWalkable) if (!checkWalkable)
return virtualOrigin; return virtualOrigin;
// check if the location is accessible // check if the location is accessible
if (canReachVector(virtualOrigin)) if (!canReachVector(origin, virtualOrigin))
continue;
if (canReachVector(virtualOrigin, target->m_vecOrigin()))
return virtualOrigin; 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 // Returns a vectors max value. For example: {123,-150, 125} = 125
float vectorMax(Vector i) float vectorMax(Vector i)
{ {
float res = fmaxf(i.x, i.y); return fmaxf(fmaxf(i.x, i.y), i.z);
return fmaxf(res, i.z);
} }
// Returns a vectors absolute value. For example {123,-150, 125} = {123,150, // 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 // check to see if we can reach a vector or if it is too high / doesn't leave
// enough space for the player // enough space for the player, optional second vector
bool canReachVector(Vector loc) bool canReachVector(Vector loc, Vector dest)
{ {
// check if the vector is too high above ground if (!dest.IsZero())
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; Vector dist = dest - loc;
// what direction to check int maxiterations = floor(dest.DistTo(loc)) / 40;
switch (i) for (int i = 0; i < maxiterations; i++)
{ {
case 0: Vector vec = loc + dist / vectorMax(vectorAbs(dist)) * 40.0f * (i + 1);
directionalLoc.x = directionalLoc.x + 40;
break; trace_t trace;
case 1: Ray_t ray;
directionalLoc.x = directionalLoc.x - 40; Vector down = vec;
break; down.z = down.z - 50;
case 2: ray.Init(vec, down);
directionalLoc.y = directionalLoc.y + 40; g_ITrace->TraceRay(ray, 0x4200400B, &trace::filter_no_player,
break; &trace);
case 3: if (!(trace.startpos.DistTo(trace.endpos) <= 45))
directionalLoc.y = directionalLoc.y - 40; return false;
break;
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; else
ray2.Init(loc, directionalLoc); {
g_ITrace->TraceRay(ray2, 0x4200400B, &trace::filter_no_player, &trace); // check if the vector is too high above ground
// distance of trace < than 26 trace_t trace;
if (trace2.startpos.DistTo(trace2.endpos) < 26.0f) 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; 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; return true;
} }
@ -230,15 +287,15 @@ bool isJumping(Vector vec)
trace_t trace; trace_t trace;
Ray_t ray; Ray_t ray;
Vector down = vec; Vector down = vec;
Vector loc = vec; Vector loc = vec;
down.z = down.z - 50; down.z = down.z - 50;
loc.z = loc.z + 5; loc.z = loc.z + 5;
ray.Init(vec, down); 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); g_ITrace->TraceRay(ray, 0x4200400B, &trace::filter_no_player, &trace);
// lower to avoid small false negatives, player can jump 42 hu according to // lower to avoid small false negatives, player can jump 42 hu according to
// the tf2 wiki, higher because loc.z = loc.z + 5; // 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 true;
return false; return false;
} }
@ -1173,7 +1230,7 @@ void PrintChat(const char *fmt, ...)
CHudBaseChat *chat = (CHudBaseChat *) g_CHUD->FindElement("CHudChat"); CHudBaseChat *chat = (CHudBaseChat *) g_CHUD->FindElement("CHudChat");
if (chat) if (chat)
{ {
std::unique_ptr<char> buf(new char[1024]); std::unique_ptr<char[]> buf(new char[1024]);
va_list list; va_list list;
va_start(list, fmt); va_start(list, fmt);
vsprintf(buf.get(), fmt, list); vsprintf(buf.get(), fmt, list);