More pathfinding improvements
This commit is contained in:
parent
668a722f8f
commit
04c6f2176c
@ -10,7 +10,7 @@ extern bool init;
|
|||||||
extern bool ReadyForCommands;
|
extern bool ReadyForCommands;
|
||||||
extern std::vector<CNavArea> areas;
|
extern std::vector<CNavArea> areas;
|
||||||
std::vector<Vector> findPath(Vector loc, Vector dest);
|
std::vector<Vector> findPath(Vector loc, Vector dest);
|
||||||
bool NavTo(Vector dest, bool navToLocalCenter = true);
|
bool NavTo(Vector dest, bool navToLocalCenter = true, bool persistent = true);
|
||||||
int findClosestNavSquare(Vector vec);
|
int findClosestNavSquare(Vector vec);
|
||||||
bool Prepare();
|
bool Prepare();
|
||||||
void CreateMove();
|
void CreateMove();
|
||||||
|
@ -35,7 +35,7 @@ bool HasLowAmmo()
|
|||||||
|
|
||||||
bool HasLowHealth()
|
bool HasLowHealth()
|
||||||
{
|
{
|
||||||
return float(LOCAL_E->m_iHealth()) / float(LOCAL_E->m_iMaxHealth()) < 0.45;
|
return float(LOCAL_E->m_iHealth()) / float(LOCAL_E->m_iMaxHealth()) < 0.64;
|
||||||
}
|
}
|
||||||
|
|
||||||
CachedEntity *nearestHealth()
|
CachedEntity *nearestHealth()
|
||||||
@ -141,7 +141,7 @@ Timer cd2{};
|
|||||||
Timer cd3{};
|
Timer cd3{};
|
||||||
void CreateMove()
|
void CreateMove()
|
||||||
{
|
{
|
||||||
if (!nav::Prepare() || !enable)
|
if ( !enable || !nav::Prepare())
|
||||||
return;
|
return;
|
||||||
if (CE_BAD(LOCAL_E) || !LOCAL_E->m_bAlivePlayer())
|
if (CE_BAD(LOCAL_E) || !LOCAL_E->m_bAlivePlayer())
|
||||||
return;
|
return;
|
||||||
|
@ -8,7 +8,7 @@ std::vector<CNavArea> areas;
|
|||||||
bool init = false;
|
bool init = false;
|
||||||
bool pathfinding = true;
|
bool pathfinding = true;
|
||||||
bool ReadyForCommands = false;
|
bool ReadyForCommands = false;
|
||||||
std::vector<std::pair<int, int>> ignores;
|
std::vector<std::pair<int, int>> ignoredConnections;
|
||||||
|
|
||||||
static settings::Bool enabled{ "misc.pathing", "true" };
|
static settings::Bool enabled{ "misc.pathing", "true" };
|
||||||
|
|
||||||
@ -27,10 +27,10 @@ struct MAP : public micropather::Graph
|
|||||||
std::unique_ptr<micropather::MicroPather> pather;
|
std::unique_ptr<micropather::MicroPather> pather;
|
||||||
bool IsIgnored(int currState, int connectionID)
|
bool IsIgnored(int currState, int connectionID)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < ignores.size(); i++)
|
for (int i = 0; i < ignoredConnections.size(); i++)
|
||||||
{
|
{
|
||||||
if (ignores.at(i).first == currState &&
|
if (ignoredConnections.at(i).first == currState &&
|
||||||
ignores.at(i).second == connectionID)
|
ignoredConnections.at(i).second == connectionID)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -157,30 +157,42 @@ void Init()
|
|||||||
}
|
}
|
||||||
pathfinding = true;
|
pathfinding = true;
|
||||||
}
|
}
|
||||||
|
std::string lastmap;
|
||||||
bool Prepare()
|
bool Prepare()
|
||||||
{
|
{
|
||||||
if (!enabled)
|
if (!enabled)
|
||||||
return false;
|
return false;
|
||||||
if (!init)
|
if (!init)
|
||||||
{
|
{
|
||||||
pathfinding = false;
|
if (lastmap == g_IEngine->GetLevelName())
|
||||||
init = true;
|
{
|
||||||
Init();
|
init = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lastmap = g_IEngine->GetLevelName();
|
||||||
|
pathfinding = false;
|
||||||
|
init = true;
|
||||||
|
Init();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!pathfinding)
|
if (!pathfinding)
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<int> localAreas(6);
|
||||||
int findClosestNavSquare(Vector vec)
|
int findClosestNavSquare(Vector vec)
|
||||||
{
|
{
|
||||||
|
if (localAreas.size() > 5)
|
||||||
|
localAreas.erase(localAreas.begin());
|
||||||
std::vector<std::pair<int, CNavArea *>> overlapping;
|
std::vector<std::pair<int, CNavArea *>> overlapping;
|
||||||
for (int i = 0; i < areas.size(); i++)
|
for (int i = 0; i < areas.size(); i++)
|
||||||
{
|
{
|
||||||
if (areas.at(i).IsOverlapping(vec))
|
if (areas.at(i).IsOverlapping(vec))
|
||||||
{
|
{
|
||||||
overlapping.push_back({ i, &areas.at(i) });
|
if (std::count(localAreas.begin(), localAreas.end(), i) < 2)
|
||||||
|
overlapping.push_back({ i, &areas.at(i) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,6 +207,26 @@ int findClosestNavSquare(Vector vec)
|
|||||||
bestSquare = overlapping.at(i).first;
|
bestSquare = overlapping.at(i).first;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (bestSquare != -1)
|
||||||
|
{
|
||||||
|
if (vec == g_pLocalPlayer->v_Origin)
|
||||||
|
localAreas.push_back(bestSquare);
|
||||||
|
return bestSquare;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < areas.size(); i++)
|
||||||
|
{
|
||||||
|
float dist = areas.at(i).m_center.DistTo(vec);
|
||||||
|
if (dist < bestDist)
|
||||||
|
{
|
||||||
|
if (std::count(localAreas.begin(), localAreas.end(), i) < 2)
|
||||||
|
{
|
||||||
|
bestDist = dist;
|
||||||
|
bestSquare = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (vec == g_pLocalPlayer->v_Origin)
|
||||||
|
localAreas.push_back(bestSquare);
|
||||||
return bestSquare;
|
return bestSquare;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,10 +256,11 @@ std::vector<Vector> findPath(Vector loc, Vector dest)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Timer inactivity{};
|
static Timer inactivity{};
|
||||||
Timer lastJump{};
|
static Timer lastJump{};
|
||||||
static std::vector<Vector> crumbs;
|
static std::vector<Vector> crumbs;
|
||||||
|
static bool ensureArrival;
|
||||||
|
|
||||||
bool NavTo(Vector dest, bool navToLocalCenter)
|
bool NavTo(Vector dest, bool navToLocalCenter, bool persistent)
|
||||||
{
|
{
|
||||||
if (CE_BAD(LOCAL_E))
|
if (CE_BAD(LOCAL_E))
|
||||||
return false;
|
return false;
|
||||||
@ -241,46 +274,57 @@ bool NavTo(Vector dest, bool navToLocalCenter)
|
|||||||
if (!navToLocalCenter)
|
if (!navToLocalCenter)
|
||||||
crumbs.erase(crumbs.begin());
|
crumbs.erase(crumbs.begin());
|
||||||
inactivity.update();
|
inactivity.update();
|
||||||
|
ensureArrival = persistent;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Vector lastArea = { 0.0f, 0.0f, 0.0f };
|
||||||
void ignoreConnection()
|
void ignoreConnection()
|
||||||
{
|
{
|
||||||
if (crumbs.size() < 1)
|
if (crumbs.size() < 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int closestArea = findClosestNavSquare(g_pLocalPlayer->v_Origin);
|
CNavArea *currnode = nullptr;
|
||||||
if (closestArea == -1)
|
for (int i = 0; i < areas.size(); i++)
|
||||||
|
{
|
||||||
|
if (areas.at(i).m_center == lastArea)
|
||||||
|
{
|
||||||
|
currnode = &areas.at(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!currnode)
|
||||||
return;
|
return;
|
||||||
CNavArea currnode = areas.at(closestArea);
|
|
||||||
|
|
||||||
CNavArea nextnode;
|
CNavArea *nextnode = nullptr;
|
||||||
for (int i = 0; i < areas.size(); i++)
|
for (int i = 0; i < areas.size(); i++)
|
||||||
{
|
{
|
||||||
if (areas.at(i).m_center == crumbs.at(0))
|
if (areas.at(i).m_center == crumbs.at(0))
|
||||||
{
|
{
|
||||||
nextnode = areas.at(i);
|
nextnode = &areas.at(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!nextnode)
|
||||||
|
return;
|
||||||
|
|
||||||
for (auto i : currnode.m_connections)
|
for (auto i : currnode->m_connections)
|
||||||
{
|
{
|
||||||
if (i.area->m_id == nextnode.m_id)
|
if (i.area->m_id == nextnode->m_id)
|
||||||
{
|
{
|
||||||
ignores.push_back({currnode.m_id, i.area->m_id});
|
ignoredConnections.push_back({ currnode->m_id, nextnode->m_id });
|
||||||
TF2MAP->pather->Reset();
|
TF2MAP->pather->Reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer ignoreReset{};
|
static Timer ignoreReset{};
|
||||||
void clearIgnores()
|
void clearIgnores()
|
||||||
{
|
{
|
||||||
if (ignoreReset.test_and_set(120000))
|
if (ignoreReset.test_and_set(180000))
|
||||||
{
|
{
|
||||||
ignores.clear();
|
ignoredConnections.clear();
|
||||||
if (TF2MAP && TF2MAP->pather)
|
if (TF2MAP && TF2MAP->pather)
|
||||||
TF2MAP->pather->Reset();
|
TF2MAP->pather->Reset();
|
||||||
}
|
}
|
||||||
@ -296,11 +340,13 @@ void CreateMove()
|
|||||||
if (crumbs.empty())
|
if (crumbs.empty())
|
||||||
{
|
{
|
||||||
ReadyForCommands = true;
|
ReadyForCommands = true;
|
||||||
|
ensureArrival = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ReadyForCommands = false;
|
ReadyForCommands = false;
|
||||||
if (g_pLocalPlayer->v_Origin.DistTo(crumbs.at(0)) < 30.0f)
|
if (g_pLocalPlayer->v_Origin.DistTo(crumbs.at(0)) < 30.0f)
|
||||||
{
|
{
|
||||||
|
lastArea = crumbs.at(0);
|
||||||
crumbs.erase(crumbs.begin());
|
crumbs.erase(crumbs.begin());
|
||||||
inactivity.update();
|
inactivity.update();
|
||||||
}
|
}
|
||||||
@ -311,24 +357,41 @@ void CreateMove()
|
|||||||
current_user_cmd->buttons |= IN_JUMP;
|
current_user_cmd->buttons |= IN_JUMP;
|
||||||
if (inactivity.test_and_set(5000))
|
if (inactivity.test_and_set(5000))
|
||||||
{
|
{
|
||||||
logging::Info("Pathing: NavBot inactive for too long. Canceling tasks and "
|
|
||||||
"ignoring connection...");
|
|
||||||
ignoreConnection();
|
ignoreConnection();
|
||||||
crumbs.clear();
|
if (ensureArrival)
|
||||||
|
{
|
||||||
|
logging::Info("Pathing: NavBot inactive for too long. Ignoring "
|
||||||
|
"connection and finding another path...");
|
||||||
|
// NavTo(crumbs.back(), true, true);
|
||||||
|
crumbs = findPath(g_pLocalPlayer->v_Origin, crumbs.back());
|
||||||
|
inactivity.update();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logging::Info(
|
||||||
|
"Pathing: NavBot inactive for too long. Canceling tasks and "
|
||||||
|
"ignoring connection...");
|
||||||
|
crumbs.clear();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
WalkTo(crumbs.at(0));
|
WalkTo(crumbs.at(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
CatCommand navinit("nav_init", "Debug nav init",
|
CatCommand navinit("nav_init", "Debug nav init", [](const CCommand &args) {
|
||||||
[](const CCommand &args) { Prepare(); });
|
lastmap = "";
|
||||||
|
init = false;
|
||||||
|
Prepare();
|
||||||
|
});
|
||||||
|
|
||||||
Vector loc;
|
Vector loc;
|
||||||
|
|
||||||
CatCommand navset("nav_set", "Debug nav set",
|
CatCommand navset("nav_set", "Debug nav set",
|
||||||
[](const CCommand &args) { loc = LOCAL_E->m_vecOrigin(); });
|
[](const CCommand &args) { loc = LOCAL_E->m_vecOrigin(); });
|
||||||
CatCommand navprint("nav_print", "Debug nav print",
|
CatCommand navprint("nav_print", "Debug nav print", [](const CCommand &args) {
|
||||||
[](const CCommand &args) { logging::Info(format(findClosestNavSquare(g_pLocalPlayer->v_Origin)).c_str()); });
|
logging::Info(
|
||||||
|
format(findClosestNavSquare(g_pLocalPlayer->v_Origin)).c_str());
|
||||||
|
});
|
||||||
|
|
||||||
CatCommand navfind("nav_find", "Debug nav find", [](const CCommand &args) {
|
CatCommand navfind("nav_find", "Debug nav find", [](const CCommand &args) {
|
||||||
std::vector<Vector> path = findPath(g_pLocalPlayer->v_Origin, loc);
|
std::vector<Vector> path = findPath(g_pLocalPlayer->v_Origin, loc);
|
||||||
@ -346,7 +409,7 @@ CatCommand navfind("nav_find", "Debug nav find", [](const CCommand &args) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
CatCommand navpath("nav_path", "Debug nav path", [](const CCommand &args) {
|
CatCommand navpath("nav_path", "Debug nav path", [](const CCommand &args) {
|
||||||
if (NavTo(loc))
|
if (NavTo(loc, true, true))
|
||||||
{
|
{
|
||||||
logging::Info("Pathing: Success! Walking to path...");
|
logging::Info("Pathing: Success! Walking to path...");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user