Working on navigation

This commit is contained in:
Vladislav Khorev committed 2026-11-15 00:51:21 +06:00
1 parent dc9ea361f0
commit 2401729c1c
82 files changed
+225 -17491

No files matched your search

+98 -17
View File
@@ -1,33 +1,114 @@
#include "navigation/PathFinder.h"
#include <algorithm>
#include <cctype>
#include <functional>
#include <limits>
#include <queue>
#include <stdexcept>
#include <utility>
#include "utils/Utils.h"
namespace FRG {
namespace {
using json = nlohmann::json;
json loadNavigationJson(const std::string& configPath, const std::string& zipPath) {
if (configPath.size() < 5 ||
std::tolower(static_cast<unsigned char>(configPath[configPath.size() - 5])) != '.' ||
std::tolower(static_cast<unsigned char>(configPath[configPath.size() - 4])) != 'j' ||
std::tolower(static_cast<unsigned char>(configPath[configPath.size() - 3])) != 's' ||
std::tolower(static_cast<unsigned char>(configPath[configPath.size() - 2])) != 'o' ||
std::tolower(static_cast<unsigned char>(configPath[configPath.size() - 1])) != 'n') {
throw std::runtime_error("Navigation config must be a JSON file: " + configPath);
}
std::string content;
try {
if (zipPath.empty()) {
content = readTextFile(configPath);
} else {
auto buffer = readFileFromZIP(configPath, zipPath);
if (buffer.empty())
throw std::runtime_error("empty result from zip");
content.assign(buffer.begin(), buffer.end());
}
} catch (const std::exception& e) {
throw std::runtime_error("Failed to read navigation config " + configPath + ": " + e.what());
}
if (content.empty())
throw std::runtime_error("Failed to read navigation config " + configPath + ": empty file");
try {
return json::parse(content);
} catch (const std::exception& e) {
throw std::runtime_error("JSON parse error in " + configPath + ": " + e.what());
}
}
std::vector<Vec2fp> parsePolygon(const json& polygon, const std::string& description) {
if (!polygon.is_array() || polygon.size() < 3)
throw std::runtime_error(description + " must contain at least three points");
std::vector<Vec2fp> result;
result.reserve(polygon.size());
for (std::size_t i = 0; i < polygon.size(); ++i) {
const auto& point = polygon.at(i);
if (!point.is_array() || point.size() != 2 ||
!point.at(0).is_number() || !point.at(1).is_number()) {
throw std::runtime_error(description + " point " + std::to_string(i) +
" must be an [x, z] numeric pair");
}
result.push_back(Vec2fp::fromFloat(
point.at(0).get<float>(), point.at(1).get<float>()));
}
return result;
}
}
void PathFinder::build(const std::string& configPath, const std::string& zipPath) {
// Здесь может быть парсинг configPath в area и obstaclePolygons.
// После заполнения геометрии строим NavMesh:
area.name = "main";
area.polygon = {
Vec2fp::fromFloat(-100.f, -100.f),
Vec2fp::fromFloat(-100.f, 100.f),
Vec2fp::fromFloat(100.f, 100.f),
Vec2fp::fromFloat(100.f, -100.f)
};
ready = false;
navMesh.clear();
area = NavigationArea{};
obstaclePolygons.clear();
floorY = fp(0);
area.available = true;
const json config = loadNavigationJson(configPath, zipPath);
if (!config.is_object())
throw std::runtime_error("Navigation config must contain a JSON object: " + configPath);
obstaclePolygons.push_back({
"p1",{ Vec2fp::fromFloat(-10.f, -10.f),
Vec2fp::fromFloat(-10.f, 10.f),
Vec2fp::fromFloat(10.f, 10.f),
Vec2fp::fromFloat(10.f, -10.f) }
});
const auto& areas = config.at("areas");
if (!areas.is_array() || areas.empty())
throw std::runtime_error("Navigation config must contain a non-empty 'areas' array: " + configPath);
//ready = true;
const auto& areaConfig = areas.at(0);
if (!areaConfig.is_object())
throw std::runtime_error("The first navigation area must be an object: " + configPath);
area.name = areaConfig.at("name").get<std::string>();
area.available = areaConfig.value("available", true);
area.polygon = parsePolygon(areaConfig.at("polygon"), "Navigation area polygon");
const auto& obstacles = config.at("obstacles");
if (!obstacles.is_array())
throw std::runtime_error("Navigation config 'obstacles' must be an array: " + configPath);
for (std::size_t i = 0; i < obstacles.size(); ++i) {
const auto& obstacleConfig = obstacles.at(i);
if (!obstacleConfig.is_object())
throw std::runtime_error("Navigation obstacle " + std::to_string(i) +
" must be an object: " + configPath);
ObstaclePolygon obstacle;
obstacle.name = obstacleConfig.at("name").get<std::string>();
obstacle.polygon = parsePolygon(
obstacleConfig.at("polygon"),
"Navigation obstacle " + std::to_string(i) + " polygon");
obstaclePolygons.push_back(std::move(obstacle));
}
floorY = fp(config.value<double>("floorY", 0.0));
generateNavMesh();
}