mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 17:03:27 +00:00
added events
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
std::expected<Vector3<float>, std::string>
|
||||
@@ -29,13 +30,40 @@ namespace omath::pathfinding
|
||||
return m_vertex_map.empty();
|
||||
}
|
||||
|
||||
void NavigationMesh::set_event(const Vector3<float>& vertex, std::string event_id)
|
||||
{
|
||||
m_vertex_events[vertex] = std::move(event_id);
|
||||
}
|
||||
|
||||
void NavigationMesh::clear_event(const Vector3<float>& vertex)
|
||||
{
|
||||
m_vertex_events.erase(vertex);
|
||||
}
|
||||
|
||||
std::optional<std::string> NavigationMesh::get_event(const Vector3<float>& vertex) const noexcept
|
||||
{
|
||||
const auto it = m_vertex_events.find(vertex);
|
||||
if (it == m_vertex_events.end())
|
||||
return std::nullopt;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Serialization format per vertex line:
|
||||
// x y z neighbor_count event_id
|
||||
// where event_id is "-" when no event is set.
|
||||
// Neighbor lines follow: nx ny nz
|
||||
|
||||
std::string NavigationMesh::serialize() const noexcept
|
||||
{
|
||||
std::ostringstream oss;
|
||||
for (const auto& [vertex, neighbors] : m_vertex_map)
|
||||
{
|
||||
const auto event_it = m_vertex_events.find(vertex);
|
||||
const std::string& event = (event_it != m_vertex_events.end()) ? event_it->second : "-";
|
||||
|
||||
oss << vertex.x << ' ' << vertex.y << ' ' << vertex.z
|
||||
<< ' ' << neighbors.size() << '\n';
|
||||
<< ' ' << neighbors.size() << ' ' << event << '\n';
|
||||
|
||||
for (const auto& n : neighbors)
|
||||
oss << n.x << ' ' << n.y << ' ' << n.z << '\n';
|
||||
}
|
||||
@@ -45,11 +73,13 @@ namespace omath::pathfinding
|
||||
void NavigationMesh::deserialize(const std::string& raw)
|
||||
{
|
||||
m_vertex_map.clear();
|
||||
m_vertex_events.clear();
|
||||
std::istringstream iss(raw);
|
||||
|
||||
Vector3<float> vertex;
|
||||
std::size_t neighbors_count;
|
||||
while (iss >> vertex.x >> vertex.y >> vertex.z >> neighbors_count)
|
||||
std::string event;
|
||||
while (iss >> vertex.x >> vertex.y >> vertex.z >> neighbors_count >> event)
|
||||
{
|
||||
std::vector<Vector3<float>> neighbors;
|
||||
neighbors.reserve(neighbors_count);
|
||||
@@ -61,6 +91,9 @@ namespace omath::pathfinding
|
||||
neighbors.push_back(n);
|
||||
}
|
||||
m_vertex_map.emplace(vertex, std::move(neighbors));
|
||||
|
||||
if (event != "-")
|
||||
m_vertex_events.emplace(vertex, std::move(event));
|
||||
}
|
||||
}
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
Reference in New Issue
Block a user