navigation_server_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*************************************************************************/
  2. /* navigation_server_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "servers/navigation_server_2d.h"
  31. #include "core/math/transform.h"
  32. #include "core/math/transform_2d.h"
  33. #include "servers/navigation_server_3d.h"
  34. /**
  35. @author AndreaCatania
  36. */
  37. NavigationServer2D *NavigationServer2D::singleton = nullptr;
  38. #define FORWARD_0_C(FUNC_NAME) \
  39. NavigationServer2D::FUNC_NAME() \
  40. const { \
  41. return NavigationServer3D::get_singleton()->FUNC_NAME(); \
  42. }
  43. #define FORWARD_1(FUNC_NAME, T_0, D_0, CONV_0) \
  44. NavigationServer2D::FUNC_NAME(T_0 D_0) { \
  45. return NavigationServer3D::get_singleton_mut()->FUNC_NAME(CONV_0(D_0)); \
  46. }
  47. #define FORWARD_1_C(FUNC_NAME, T_0, D_0, CONV_0) \
  48. NavigationServer2D::FUNC_NAME(T_0 D_0) \
  49. const { \
  50. return NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0)); \
  51. }
  52. #define FORWARD_2_C(FUNC_NAME, T_0, D_0, T_1, D_1, CONV_0, CONV_1) \
  53. NavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1) \
  54. const { \
  55. return NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1)); \
  56. }
  57. #define FORWARD_2_R_C(CONV_R, FUNC_NAME, T_0, D_0, T_1, D_1, CONV_0, CONV_1) \
  58. NavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1) \
  59. const { \
  60. return CONV_R(NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1))); \
  61. }
  62. #define FORWARD_4_R_C(CONV_R, FUNC_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3, CONV_0, CONV_1, CONV_2, CONV_3) \
  63. NavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) \
  64. const { \
  65. return CONV_R(NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1), CONV_2(D_2), CONV_3(D_3))); \
  66. }
  67. #define FORWARD_4_C(FUNC_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3, CONV_0, CONV_1, CONV_2, CONV_3) \
  68. NavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) \
  69. const { \
  70. return NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1), CONV_2(D_2), CONV_3(D_3)); \
  71. }
  72. static RID rid_to_rid(const RID d) {
  73. return d;
  74. }
  75. static bool bool_to_bool(const bool d) {
  76. return d;
  77. }
  78. static int int_to_int(const int d) {
  79. return d;
  80. }
  81. static real_t real_to_real(const real_t d) {
  82. return d;
  83. }
  84. static Vector3 v2_to_v3(const Vector2 d) {
  85. return Vector3(d.x, 0.0, d.y);
  86. }
  87. static Vector2 v3_to_v2(const Vector3 &d) {
  88. return Vector2(d.x, d.z);
  89. }
  90. static Vector<Vector2> vector_v3_to_v2(const Vector<Vector3> &d) {
  91. Vector<Vector2> nd;
  92. nd.resize(d.size());
  93. for (int i(0); i < nd.size(); i++) {
  94. nd.write[i] = v3_to_v2(d[i]);
  95. }
  96. return nd;
  97. }
  98. static Transform trf2_to_trf3(const Transform2D &d) {
  99. Vector3 o(v2_to_v3(d.get_origin()));
  100. Basis b;
  101. b.rotate(Vector3(0, 1, 0), d.get_rotation());
  102. return Transform(b, o);
  103. }
  104. static Object *obj_to_obj(Object *d) {
  105. return d;
  106. }
  107. static StringName sn_to_sn(StringName &d) {
  108. return d;
  109. }
  110. static Variant var_to_var(Variant &d) {
  111. return d;
  112. }
  113. static Ref<NavigationMesh> poly_to_mesh(Ref<NavigationPolygon> d) {
  114. if (d.is_valid()) {
  115. return d->get_mesh();
  116. } else {
  117. return Ref<NavigationMesh>();
  118. }
  119. }
  120. void NavigationServer2D::_bind_methods() {
  121. ClassDB::bind_method(D_METHOD("map_create"), &NavigationServer2D::map_create);
  122. ClassDB::bind_method(D_METHOD("map_set_active", "map", "active"), &NavigationServer2D::map_set_active);
  123. ClassDB::bind_method(D_METHOD("map_is_active", "nap"), &NavigationServer2D::map_is_active);
  124. ClassDB::bind_method(D_METHOD("map_set_cell_size", "map", "cell_size"), &NavigationServer2D::map_set_cell_size);
  125. ClassDB::bind_method(D_METHOD("map_get_cell_size", "map"), &NavigationServer2D::map_get_cell_size);
  126. ClassDB::bind_method(D_METHOD("map_set_edge_connection_margin", "map", "margin"), &NavigationServer2D::map_set_edge_connection_margin);
  127. ClassDB::bind_method(D_METHOD("map_get_edge_connection_margin", "map"), &NavigationServer2D::map_get_edge_connection_margin);
  128. ClassDB::bind_method(D_METHOD("map_get_path", "map", "origin", "destination", "optimize"), &NavigationServer2D::map_get_path);
  129. ClassDB::bind_method(D_METHOD("map_get_closest_point", "map", "to_point"), &NavigationServer2D::map_get_closest_point);
  130. ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer2D::map_get_closest_point_owner);
  131. ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer2D::region_create);
  132. ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer2D::region_set_map);
  133. ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer2D::region_set_transform);
  134. ClassDB::bind_method(D_METHOD("region_set_navpoly", "region", "nav_poly"), &NavigationServer2D::region_set_navpoly);
  135. ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer2D::agent_create);
  136. ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer2D::agent_set_map);
  137. ClassDB::bind_method(D_METHOD("agent_set_neighbor_dist", "agent", "dist"), &NavigationServer2D::agent_set_neighbor_dist);
  138. ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer2D::agent_set_max_neighbors);
  139. ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &NavigationServer2D::agent_set_time_horizon);
  140. ClassDB::bind_method(D_METHOD("agent_set_radius", "agent", "radius"), &NavigationServer2D::agent_set_radius);
  141. ClassDB::bind_method(D_METHOD("agent_set_max_speed", "agent", "max_speed"), &NavigationServer2D::agent_set_max_speed);
  142. ClassDB::bind_method(D_METHOD("agent_set_velocity", "agent", "velocity"), &NavigationServer2D::agent_set_velocity);
  143. ClassDB::bind_method(D_METHOD("agent_set_target_velocity", "agent", "target_velocity"), &NavigationServer2D::agent_set_target_velocity);
  144. ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer2D::agent_set_position);
  145. ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer2D::agent_is_map_changed);
  146. ClassDB::bind_method(D_METHOD("agent_set_callback", "agent", "receiver", "method", "userdata"), &NavigationServer2D::agent_set_callback, DEFVAL(Variant()));
  147. ClassDB::bind_method(D_METHOD("free", "object"), &NavigationServer2D::free);
  148. }
  149. NavigationServer2D::NavigationServer2D() {
  150. singleton = this;
  151. }
  152. NavigationServer2D::~NavigationServer2D() {
  153. singleton = nullptr;
  154. }
  155. RID FORWARD_0_C(map_create);
  156. void FORWARD_2_C(map_set_active, RID, p_map, bool, p_active, rid_to_rid, bool_to_bool);
  157. bool FORWARD_1_C(map_is_active, RID, p_map, rid_to_rid);
  158. void FORWARD_2_C(map_set_cell_size, RID, p_map, real_t, p_cell_size, rid_to_rid, real_to_real);
  159. real_t FORWARD_1_C(map_get_cell_size, RID, p_map, rid_to_rid);
  160. void FORWARD_2_C(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin, rid_to_rid, real_to_real);
  161. real_t FORWARD_1_C(map_get_edge_connection_margin, RID, p_map, rid_to_rid);
  162. Vector<Vector2> FORWARD_4_R_C(vector_v3_to_v2, map_get_path, RID, p_map, Vector2, p_origin, Vector2, p_destination, bool, p_optimize, rid_to_rid, v2_to_v3, v2_to_v3, bool_to_bool);
  163. Vector2 FORWARD_2_R_C(v3_to_v2, map_get_closest_point, RID, p_map, const Vector2 &, p_point, rid_to_rid, v2_to_v3);
  164. RID FORWARD_2_C(map_get_closest_point_owner, RID, p_map, const Vector2 &, p_point, rid_to_rid, v2_to_v3);
  165. RID FORWARD_0_C(region_create);
  166. void FORWARD_2_C(region_set_map, RID, p_region, RID, p_map, rid_to_rid, rid_to_rid);
  167. void FORWARD_2_C(region_set_transform, RID, p_region, Transform2D, p_transform, rid_to_rid, trf2_to_trf3);
  168. void NavigationServer2D::region_set_navpoly(RID p_region, Ref<NavigationPolygon> p_nav_mesh) const {
  169. NavigationServer3D::get_singleton()->region_set_navmesh(p_region, poly_to_mesh(p_nav_mesh));
  170. }
  171. RID NavigationServer2D::agent_create() const {
  172. RID agent = NavigationServer3D::get_singleton()->agent_create();
  173. NavigationServer3D::get_singleton()->agent_set_ignore_y(agent, true);
  174. return agent;
  175. }
  176. void FORWARD_2_C(agent_set_map, RID, p_agent, RID, p_map, rid_to_rid, rid_to_rid);
  177. void FORWARD_2_C(agent_set_neighbor_dist, RID, p_agent, real_t, p_dist, rid_to_rid, real_to_real);
  178. void FORWARD_2_C(agent_set_max_neighbors, RID, p_agent, int, p_count, rid_to_rid, int_to_int);
  179. void FORWARD_2_C(agent_set_time_horizon, RID, p_agent, real_t, p_time, rid_to_rid, real_to_real);
  180. void FORWARD_2_C(agent_set_radius, RID, p_agent, real_t, p_radius, rid_to_rid, real_to_real);
  181. void FORWARD_2_C(agent_set_max_speed, RID, p_agent, real_t, p_max_speed, rid_to_rid, real_to_real);
  182. void FORWARD_2_C(agent_set_velocity, RID, p_agent, Vector2, p_velocity, rid_to_rid, v2_to_v3);
  183. void FORWARD_2_C(agent_set_target_velocity, RID, p_agent, Vector2, p_velocity, rid_to_rid, v2_to_v3);
  184. void FORWARD_2_C(agent_set_position, RID, p_agent, Vector2, p_position, rid_to_rid, v2_to_v3);
  185. void FORWARD_2_C(agent_set_ignore_y, RID, p_agent, bool, p_ignore, rid_to_rid, bool_to_bool);
  186. bool FORWARD_1_C(agent_is_map_changed, RID, p_agent, rid_to_rid);
  187. void FORWARD_4_C(agent_set_callback, RID, p_agent, Object *, p_receiver, StringName, p_method, Variant, p_udata, rid_to_rid, obj_to_obj, sn_to_sn, var_to_var);
  188. void FORWARD_1_C(free, RID, p_object, rid_to_rid);