2020-01-10 19:22:34 +08:00
|
|
|
/**************************************************************************/
|
2020-02-11 21:01:43 +08:00
|
|
|
/* nav_utils.h */
|
2020-01-10 19:22:34 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2020-02-11 21:01:43 +08:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2020-01-10 19:22:34 +08:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef NAV_UTILS_H
|
|
|
|
#define NAV_UTILS_H
|
|
|
|
|
|
|
|
#include "core/math/vector3.h"
|
2022-05-13 21:04:37 +08:00
|
|
|
#include "core/templates/hash_map.h"
|
|
|
|
#include "core/templates/hashfuncs.h"
|
2022-07-29 01:24:14 +08:00
|
|
|
#include "core/templates/local_vector.h"
|
2020-01-10 19:22:34 +08:00
|
|
|
|
2022-01-31 07:39:52 +08:00
|
|
|
class NavBase;
|
2020-01-10 19:22:34 +08:00
|
|
|
|
|
|
|
namespace gd {
|
|
|
|
struct Polygon;
|
|
|
|
|
|
|
|
union PointKey {
|
|
|
|
struct {
|
|
|
|
int64_t x : 21;
|
|
|
|
int64_t y : 22;
|
|
|
|
int64_t z : 21;
|
|
|
|
};
|
|
|
|
|
2021-02-08 17:57:18 +08:00
|
|
|
uint64_t key = 0;
|
2020-01-10 19:22:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EdgeKey {
|
|
|
|
PointKey a;
|
|
|
|
PointKey b;
|
|
|
|
|
2022-05-13 21:04:37 +08:00
|
|
|
static uint32_t hash(const EdgeKey &p_val) {
|
|
|
|
return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const EdgeKey &p_key) const {
|
|
|
|
return (a.key == p_key.a.key) && (b.key == p_key.b.key);
|
2020-01-10 19:22:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :
|
|
|
|
a(p_a),
|
|
|
|
b(p_b) {
|
|
|
|
if (a.key > b.key) {
|
|
|
|
SWAP(a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Point {
|
|
|
|
Vector3 pos;
|
|
|
|
PointKey key;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Edge {
|
2021-03-15 19:45:28 +08:00
|
|
|
/// The gateway in the edge, as, in some case, the whole edge might not be navigable.
|
|
|
|
struct Connection {
|
2022-01-31 07:39:52 +08:00
|
|
|
/// Polygon that this connection leads to.
|
2021-03-15 19:45:28 +08:00
|
|
|
Polygon *polygon = nullptr;
|
2022-01-31 07:39:52 +08:00
|
|
|
|
|
|
|
/// Edge of the source polygon where this connection starts from.
|
2021-03-15 19:45:28 +08:00
|
|
|
int edge = -1;
|
2022-01-31 07:39:52 +08:00
|
|
|
|
|
|
|
/// Point on the edge where the gateway leading to the poly starts.
|
2021-03-15 19:45:28 +08:00
|
|
|
Vector3 pathway_start;
|
2022-01-31 07:39:52 +08:00
|
|
|
|
|
|
|
/// Point on the edge where the gateway leading to the poly ends.
|
2021-03-15 19:45:28 +08:00
|
|
|
Vector3 pathway_end;
|
|
|
|
};
|
2022-01-31 07:39:52 +08:00
|
|
|
|
|
|
|
/// Connections from this edge to other polygons.
|
2021-03-15 19:45:28 +08:00
|
|
|
Vector<Connection> connections;
|
2020-01-10 19:22:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Polygon {
|
2022-01-31 07:39:52 +08:00
|
|
|
/// Navigation region or link that contains this polygon.
|
|
|
|
const NavBase *owner = nullptr;
|
2020-01-10 19:22:34 +08:00
|
|
|
|
|
|
|
/// The points of this `Polygon`
|
2022-07-29 01:24:14 +08:00
|
|
|
LocalVector<Point> points;
|
2020-01-10 19:22:34 +08:00
|
|
|
|
2022-01-31 07:39:52 +08:00
|
|
|
/// Are the points clockwise?
|
2020-01-10 19:22:34 +08:00
|
|
|
bool clockwise;
|
|
|
|
|
|
|
|
/// The edges of this `Polygon`
|
2022-07-29 01:24:14 +08:00
|
|
|
LocalVector<Edge> edges;
|
2020-01-10 19:22:34 +08:00
|
|
|
|
|
|
|
/// The center of this `Polygon`
|
|
|
|
Vector3 center;
|
2023-10-24 00:16:05 +08:00
|
|
|
|
|
|
|
real_t surface_area = 0.0;
|
2020-01-10 19:22:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct NavigationPoly {
|
2020-05-12 23:01:17 +08:00
|
|
|
uint32_t self_id = 0;
|
2020-01-10 19:22:34 +08:00
|
|
|
/// This poly.
|
|
|
|
const Polygon *poly;
|
2021-03-15 19:45:28 +08:00
|
|
|
|
|
|
|
/// Those 4 variables are used to travel the path backwards.
|
|
|
|
int back_navigation_poly_id = -1;
|
2022-01-31 07:39:52 +08:00
|
|
|
int back_navigation_edge = -1;
|
2021-03-15 19:45:28 +08:00
|
|
|
Vector3 back_navigation_edge_pathway_start;
|
|
|
|
Vector3 back_navigation_edge_pathway_end;
|
|
|
|
|
2022-12-07 06:32:11 +08:00
|
|
|
/// The entry position of this poly.
|
2020-01-10 19:22:34 +08:00
|
|
|
Vector3 entry;
|
|
|
|
/// The distance to the destination.
|
2023-03-07 22:16:07 +08:00
|
|
|
real_t traveled_distance = 0.0;
|
2020-01-10 19:22:34 +08:00
|
|
|
|
2022-07-29 01:24:14 +08:00
|
|
|
NavigationPoly() { poly = nullptr; }
|
|
|
|
|
2020-01-10 19:22:34 +08:00
|
|
|
NavigationPoly(const Polygon *p_poly) :
|
2020-05-12 23:01:17 +08:00
|
|
|
poly(p_poly) {}
|
2020-01-10 19:22:34 +08:00
|
|
|
|
|
|
|
bool operator==(const NavigationPoly &other) const {
|
2024-01-29 04:51:39 +08:00
|
|
|
return poly == other.poly;
|
2020-01-10 19:22:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const NavigationPoly &other) const {
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-13 23:07:01 +08:00
|
|
|
struct ClosestPointQueryResult {
|
|
|
|
Vector3 point;
|
|
|
|
Vector3 normal;
|
|
|
|
RID owner;
|
|
|
|
};
|
|
|
|
|
2020-01-10 19:22:34 +08:00
|
|
|
} // namespace gd
|
|
|
|
|
|
|
|
#endif // NAV_UTILS_H
|