WIP: Pathfinding Nr. 4

This commit is contained in:
TotallyNotElite 2018-08-15 19:41:47 +02:00
parent ea4dfb53d0
commit 0054b64cb3
4 changed files with 54 additions and 56 deletions

View File

@ -110,10 +110,6 @@ target_include_directories(cathook SYSTEM PRIVATE
"external/source-sdk-2013-headers/mp/src/tier1" "external/source-sdk-2013-headers/mp/src/tier1"
"external/source-sdk-2013-headers/mp/src") "external/source-sdk-2013-headers/mp/src")
target_include_directories(cathook PRIVATE "external/ucccccp")
target_include_directories(cathook PRIVATE "external/MicroPather")
target_include_directories(cathook PRIVATE "external/TF2_NavFile_Reader")
if(EnableWarnings) if(EnableWarnings)
target_compile_options(cathook PRIVATE -Wall -Wextra) target_compile_options(cathook PRIVATE -Wall -Wextra)
else() else()
@ -141,7 +137,7 @@ endif()
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(include) add_subdirectory(include)
add_subdirectory(external/MicroPather) add_subdirectory(external)
add_custom_command(TARGET cathook POST_BUILD add_custom_command(TARGET cathook POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:cathook> "${CMAKE_SOURCE_DIR}/bin/$<TARGET_FILE_NAME:cathook>") COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:cathook> "${CMAKE_SOURCE_DIR}/bin/$<TARGET_FILE_NAME:cathook>")

6
external/CMakeLists.txt vendored Executable file
View File

@ -0,0 +1,6 @@
target_sources(cathook PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/MicroPather/micropather.cpp")
target_include_directories(cathook PRIVATE "${CMAKE_CURRENT_LIST_DIR}/MicroPather")
target_include_directories(cathook PRIVATE "${CMAKE_CURRENT_LIST_DIR}/ucccccp")
target_include_directories(cathook PRIVATE "${CMAKE_CURRENT_LIST_DIR}/TF2_NavFile_Reader")

@ -1 +1 @@
Subproject commit 65818ea2cda4fa3f8e3f17e272692543f704d1f4 Subproject commit d6816a1b67768227c7ed839fb7278fa47b63f643

View File

@ -1,30 +1,43 @@
#include "common.hpp" #include "common.hpp"
#include "micropather.h" #include "micropather.h"
#include "pwd.h"
namespace nav namespace nav
{ {
CNavFile navfile(nullptr); static CNavFile navfile(nullptr);
CNavArea LocalNav; // Todo: CNavArea* to navfile
std::vector<CNavArea> areas; static std::vector<CNavArea> areas;
// std::vector<CNavArea> SniperAreas; // std::vector<CNavArea> SniperAreas;
// Todo fix
int FindInVector(int id)
{
for (int i = 0; i < areas.size(); i++)
{
if (areas.at(i).m_id == id)
return i;
}
}
struct MAP : public micropather::Graph struct MAP : public micropather::Graph
{ {
float LeastCostEstimate(void *stateStart, void *stateEnd) float LeastCostEstimate(void *stateStart, void *stateEnd)
{ {
int *start = (int *) (stateStart); CNavArea *start = static_cast<CNavArea *>(stateStart);
int *end = (int *) (stateEnd); CNavArea *end = static_cast<CNavArea *>(stateEnd);
return areas.at(*start).m_center.DistTo(areas.at(*end).m_center); float dist = start->m_center.DistTo(end->m_center);
return dist;
} }
void AdjacentCost(void *state, void AdjacentCost(void *state, MP_VECTOR<micropather::StateCost> *adjacent)
MP_VECTOR<micropather::StateCost> *adjacent)
{ {
int *area = (int *) (state); CNavArea *area = static_cast<CNavArea *>(state);
auto &neighbours = areas.at(*area).m_connections; auto &neighbours = area->m_connections;
for (auto i : neighbours) for (auto i : neighbours)
{ {
adjacent->push_back(micropather::StateCost{ micropather::StateCost cost;
(void *) (i.area->m_id), cost.state =
i.area->m_center.DistTo(areas.at(*area).m_center) }); static_cast<void *>(&areas.at(FindInVector(i.area->m_id)));
cost.cost = area->m_center.DistTo(i.area->m_center);
adjacent->push_back(cost);
} }
} }
void PrintStateInfo(void *state) void PrintStateInfo(void *state)
@ -38,31 +51,31 @@ struct MAP : public micropather::Graph
virtual ~MAP() virtual ~MAP()
{ {
} }
}; };
void Init() void Init()
{ {
// TODO: Improve performance // TODO: Improve performance
// std::string lvlname(g_IEngine->GetLevelName()); std::string lvlname(g_IEngine->GetLevelName());
// int dotpos = lvlname.find('.'); int dotpos = lvlname.find('.');
// lvlname = lvlname.substr(0, dotpos); lvlname = lvlname.substr(0, dotpos);
// std::string lvldir("/home/elite/Schreibtisch/tf2/maps/"); std::string lvldir("/home/");
// lvldir.append(lvlname); passwd *pwd = getpwuid(getuid());
// lvldir.append(".nav"); lvldir.append(pwd->pw_name);
// FIXME temp lvldir.append("/.steam/steam/steamapps/common/Team Fortress 2/tf/");
std::string lvldir = "/home/elite/Schreibtisch/tf2/maps/cp_dustbowl.nav"; lvldir.append(lvlname);
lvldir.append(".nav");
logging::Info(lvldir.c_str());
for (auto &it : areas) areas.empty();
it = {};
navfile = CNavFile(lvldir.c_str()); navfile = CNavFile(lvldir.c_str());
if (!navfile.m_isOK) if (!navfile.m_isOK)
logging::Info("Invalid Nav File"); logging::Info("Invalid Nav File");
else else
{ {
areas.empty(); areas.reserve(navfile.m_areas.size());
for (auto i : navfile.m_areas) for (auto i : navfile.m_areas)
areas.push_back(i); areas.push_back(i);
} }
@ -98,14 +111,18 @@ std::vector<Vector> findPath(Vector loc, Vector dest)
logging::Info("Initiating map"); logging::Info("Initiating map");
MAP TF2MAP; MAP TF2MAP;
logging::Info("Initiating pather"); logging::Info("Initiating pather");
micropather::MicroPather pather(&TF2MAP, 5000, 8, true); // Todo: Make MicroPather a member of TF2MAP
micropather::MicroPather pather(&TF2MAP, areas.size(), 8, true);
logging::Info("Solving"); logging::Info("Solving");
pather.Solve((void *) (&id_loc), (void *) (&id_dest), &pathNodes, &cost); int result = pather.Solve(static_cast<void *>(&areas.at(id_loc)),
static_cast<void *>(&areas.at(id_dest)),
&pathNodes, &cost);
logging::Info(format("Result:", result).c_str());
logging::Info("Converting to vector"); logging::Info("Converting to vector");
std::vector<Vector> path; std::vector<Vector> path;
for (int i = 0; i < pathNodes.size(); i++) for (int i = 0; i < pathNodes.size(); i++)
{ {
path.push_back(areas.at(*(int *) (pathNodes[i])).m_center); path.push_back(static_cast<CNavArea *>(pathNodes[i])->m_center);
} }
return path; return path;
} }
@ -123,6 +140,7 @@ CatCommand navfind("nav_find", "Debug nav find", [](const CCommand &args) {
if (path.empty()) if (path.empty())
{ {
logging::Info("Pathing: No path found"); logging::Info("Pathing: No path found");
return;
} }
std::string output = "Pathing: Path found! Path: "; std::string output = "Pathing: Path found! Path: ";
for (int i = 0; i < path.size(); i++) for (int i = 0; i < path.size(); i++)
@ -132,26 +150,4 @@ CatCommand navfind("nav_find", "Debug nav find", [](const CCommand &args) {
logging::Info(output.c_str()); logging::Info(output.c_str());
}); });
// Timer cd{};
// void CreateMove()
// {
// if (navfile.m_isOK)
// {
// if (cd.test_and_set(300)) {
// for (auto i : navfile.m_areas) {
// Vector vec = LOCAL_E->m_vecOrigin();
// if (i.Contains(vec)) {
// LocalNav = i;
// break;
// }
// }
// }
// if (SniperAreas.size()) {
// auto res =
// areas[LocalNav.m_id]->FindPath(areas[SniperAreas[0].m_id]);
// for (auto r : res)
// logging::Info("%f, %f, %f", r->pos.x, r->pos.y, r->pos.z);
// }
// }
// }
} // namespace nav } // namespace nav