WIP: Pathfinding
This commit is contained in:
parent
d417b2b778
commit
3aebbc1c4a
6
.gitmodules
vendored
Executable file → Normal file
6
.gitmodules
vendored
Executable file → Normal file
@ -16,3 +16,9 @@
|
||||
[submodule "external/co-library"]
|
||||
path = external/co-library
|
||||
url = https://github.com/nullworks/co-library.git
|
||||
[submodule "external/TF2_NavFile_Reader"]
|
||||
path = external/TF2_NavFile_Reader
|
||||
url = https://github.com/nullworks/TF2_NavFile_Reader
|
||||
[submodule "external/PathFinder"]
|
||||
path = external/PathFinder
|
||||
url = https://github.com/Sahnvour/PathFinder
|
||||
|
1
external/PathFinder
vendored
Submodule
1
external/PathFinder
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 03bac25118aaa65822d036ac9f3b82124b681ad9
|
1
external/TF2_NavFile_Reader
vendored
Submodule
1
external/TF2_NavFile_Reader
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 3514e4821928dbdabe26ce324bf4f510380ad007
|
@ -33,7 +33,8 @@ target_sources(cathook PRIVATE
|
||||
"${CMAKE_CURRENT_LIST_DIR}/votelogger.cpp"
|
||||
"${CMAKE_CURRENT_LIST_DIR}/MiscTemporary.cpp"
|
||||
"${CMAKE_CURRENT_LIST_DIR}/Options.cpp"
|
||||
"${CMAKE_CURRENT_LIST_DIR}/PlayerTools.cpp")
|
||||
"${CMAKE_CURRENT_LIST_DIR}/PlayerTools.cpp"
|
||||
"${CMAKE_CURRENT_LIST_DIR}/pathfinder.cpp")
|
||||
|
||||
add_subdirectory(core)
|
||||
add_subdirectory(classinfo)
|
||||
@ -47,4 +48,4 @@ add_subdirectory(settings)
|
||||
|
||||
if(EnableVisuals)
|
||||
add_subdirectory(visual)
|
||||
endif()
|
||||
endif()
|
||||
|
101
src/pathfinder.cpp
Normal file
101
src/pathfinder.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
#include "common.hpp"
|
||||
#include "external/TF2_NavFile_Reader/CNavFile.h"
|
||||
#include "external/PathFinder/src/PathFinder.h"
|
||||
#include "external/PathFinder/src/AStar.h"
|
||||
|
||||
struct navNode : public AStarNode
|
||||
{
|
||||
navNode()
|
||||
{
|
||||
}
|
||||
|
||||
~navNode()
|
||||
{
|
||||
}
|
||||
|
||||
Vector vecLoc;
|
||||
};
|
||||
|
||||
std::unique_ptr<CNavFile> navData = nullptr;
|
||||
std::vector<navNode *> nodes;
|
||||
Timer pathtimer{};
|
||||
settings::Bool enabled{"pathing.enabled", 0};
|
||||
|
||||
bool init()
|
||||
{
|
||||
if(!enabled)
|
||||
return false;
|
||||
logging::Info("Pathing: Initiating path...");
|
||||
|
||||
// This will not work, please fix
|
||||
std::string levelName = g_IEngine->GetLevelName();
|
||||
navData = std::make_unique<CNavFile>(CNavFile(levelName.c_str()));
|
||||
if (!navData->m_isOK)
|
||||
{
|
||||
navData = nullptr;
|
||||
logging::Info("Pathing: Failed to parse nav file!");
|
||||
|
||||
return false;
|
||||
}
|
||||
std::vector<CNavArea> *areas = &navData->m_areas;
|
||||
int nodeCount = areas->size();
|
||||
|
||||
nodes.clear();
|
||||
nodes.reserve(nodeCount);
|
||||
|
||||
// register nodes
|
||||
for (int i = 0; i < nodeCount; i++)
|
||||
{
|
||||
navNode *node{};
|
||||
//node->setPosition(areas->at(i).m_center.x, areas->at(i).m_center.y);
|
||||
node->vecLoc = areas->at(i).m_center;
|
||||
nodes.push_back(node);
|
||||
}
|
||||
|
||||
for (int i = 0; i < nodeCount; ++i)
|
||||
{
|
||||
std::vector<NavConnect> *connections = &areas->at(i).m_connections;
|
||||
int childCount = connections->size();
|
||||
navNode *currNode = nodes.at(i);
|
||||
for (int j = 0; j < childCount; j++)
|
||||
{
|
||||
currNode->addChild(nodes.at(connections->at(j).id), 1.0f);
|
||||
}
|
||||
}
|
||||
logging::Info("Path init successful");
|
||||
return true;
|
||||
}
|
||||
|
||||
int findClosestNavSquare(Vector vec)
|
||||
{
|
||||
float bestDist = 999999.0f;
|
||||
int bestSquare = -1;
|
||||
for (int i = 0; i < nodes.size(); i++)
|
||||
{
|
||||
float dist = nodes.at(i)->vecLoc.DistTo(vec);
|
||||
if(dist < bestDist)
|
||||
{
|
||||
bestDist = dist;
|
||||
bestSquare = i;
|
||||
}
|
||||
}
|
||||
return bestSquare;
|
||||
}
|
||||
|
||||
std::vector<Vector> findPath(Vector loc, Vector dest)
|
||||
{
|
||||
if (nodes.empty())
|
||||
return {};
|
||||
|
||||
int node_loc = findClosestNavSquare(loc);
|
||||
int node_dest = findClosestNavSquare(dest);
|
||||
|
||||
PathFinder<navNode> Finder;
|
||||
Finder.setStart(*nodes.at(node_loc));
|
||||
Finder.setGoal(*nodes.at(node_dest));
|
||||
|
||||
std::vector<navNode> pathNodes;
|
||||
//bool result = Finder.findPath<AStar>(pathNodes);
|
||||
std::vector<Vector> path;
|
||||
|
||||
}
|
Reference in New Issue
Block a user