multiplayer_synchronizer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*************************************************************************/
  2. /* multiplayer_synchronizer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "multiplayer_synchronizer.h"
  31. #include "core/config/engine.h"
  32. #include "scene/main/multiplayer_api.h"
  33. Object *MultiplayerSynchronizer::_get_prop_target(Object *p_obj, const NodePath &p_path) {
  34. if (p_path.get_name_count() == 0) {
  35. return p_obj;
  36. }
  37. Node *node = Object::cast_to<Node>(p_obj);
  38. ERR_FAIL_COND_V_MSG(!node || !node->has_node(p_path), nullptr, vformat("Node '%s' not found.", p_path));
  39. return node->get_node(p_path);
  40. }
  41. void MultiplayerSynchronizer::_stop() {
  42. #ifdef TOOLS_ENABLED
  43. if (Engine::get_singleton()->is_editor_hint()) {
  44. return;
  45. }
  46. #endif
  47. root_node_cache = ObjectID();
  48. reset();
  49. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  50. if (node) {
  51. get_multiplayer()->object_configuration_remove(node, this);
  52. }
  53. }
  54. void MultiplayerSynchronizer::_start() {
  55. #ifdef TOOLS_ENABLED
  56. if (Engine::get_singleton()->is_editor_hint()) {
  57. return;
  58. }
  59. #endif
  60. root_node_cache = ObjectID();
  61. reset();
  62. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  63. if (node) {
  64. root_node_cache = node->get_instance_id();
  65. get_multiplayer()->object_configuration_add(node, this);
  66. _update_process();
  67. }
  68. }
  69. void MultiplayerSynchronizer::_update_process() {
  70. #ifdef TOOLS_ENABLED
  71. if (Engine::get_singleton()->is_editor_hint()) {
  72. return;
  73. }
  74. #endif
  75. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  76. if (!node) {
  77. return;
  78. }
  79. set_process_internal(false);
  80. set_physics_process_internal(false);
  81. if (!visibility_filters.size()) {
  82. return;
  83. }
  84. switch (visibility_update_mode) {
  85. case VISIBILITY_PROCESS_IDLE:
  86. set_process_internal(true);
  87. break;
  88. case VISIBILITY_PROCESS_PHYSICS:
  89. set_physics_process_internal(true);
  90. break;
  91. case VISIBILITY_PROCESS_NONE:
  92. break;
  93. }
  94. }
  95. Node *MultiplayerSynchronizer::get_root_node() {
  96. return root_node_cache.is_valid() ? Object::cast_to<Node>(ObjectDB::get_instance(root_node_cache)) : nullptr;
  97. }
  98. void MultiplayerSynchronizer::reset() {
  99. net_id = 0;
  100. last_sync_msec = 0;
  101. last_inbound_sync = 0;
  102. }
  103. uint32_t MultiplayerSynchronizer::get_net_id() const {
  104. return net_id;
  105. }
  106. void MultiplayerSynchronizer::set_net_id(uint32_t p_net_id) {
  107. net_id = p_net_id;
  108. }
  109. bool MultiplayerSynchronizer::update_outbound_sync_time(uint64_t p_msec) {
  110. if (p_msec >= last_sync_msec + interval_msec) {
  111. last_sync_msec = p_msec;
  112. return true;
  113. }
  114. return false;
  115. }
  116. bool MultiplayerSynchronizer::update_inbound_sync_time(uint16_t p_network_time) {
  117. if (p_network_time <= last_inbound_sync && last_inbound_sync - p_network_time < 32767) {
  118. return false;
  119. }
  120. last_inbound_sync = p_network_time;
  121. return true;
  122. }
  123. PackedStringArray MultiplayerSynchronizer::get_configuration_warnings() const {
  124. PackedStringArray warnings = Node::get_configuration_warnings();
  125. if (root_path.is_empty() || !has_node(root_path)) {
  126. warnings.push_back(RTR("A valid NodePath must be set in the \"Root Path\" property in order for MultiplayerSynchronizer to be able to synchronize properties."));
  127. }
  128. return warnings;
  129. }
  130. Error MultiplayerSynchronizer::get_state(const List<NodePath> &p_properties, Object *p_obj, Vector<Variant> &r_variant, Vector<const Variant *> &r_variant_ptrs) {
  131. ERR_FAIL_COND_V(!p_obj, ERR_INVALID_PARAMETER);
  132. r_variant.resize(p_properties.size());
  133. r_variant_ptrs.resize(r_variant.size());
  134. int i = 0;
  135. for (const NodePath &prop : p_properties) {
  136. bool valid = false;
  137. const Object *obj = _get_prop_target(p_obj, prop);
  138. ERR_FAIL_COND_V(!obj, FAILED);
  139. r_variant.write[i] = obj->get(prop.get_concatenated_subnames(), &valid);
  140. r_variant_ptrs.write[i] = &r_variant[i];
  141. ERR_FAIL_COND_V_MSG(!valid, ERR_INVALID_DATA, vformat("Property '%s' not found.", prop));
  142. i++;
  143. }
  144. return OK;
  145. }
  146. Error MultiplayerSynchronizer::set_state(const List<NodePath> &p_properties, Object *p_obj, const Vector<Variant> &p_state) {
  147. ERR_FAIL_COND_V(!p_obj, ERR_INVALID_PARAMETER);
  148. int i = 0;
  149. for (const NodePath &prop : p_properties) {
  150. Object *obj = _get_prop_target(p_obj, prop);
  151. ERR_FAIL_COND_V(!obj, FAILED);
  152. obj->set(prop.get_concatenated_subnames(), p_state[i]);
  153. i += 1;
  154. }
  155. return OK;
  156. }
  157. bool MultiplayerSynchronizer::is_visibility_public() const {
  158. return peer_visibility.has(0);
  159. }
  160. void MultiplayerSynchronizer::set_visibility_public(bool p_visible) {
  161. set_visibility_for(0, p_visible);
  162. }
  163. bool MultiplayerSynchronizer::is_visible_to(int p_peer) {
  164. if (visibility_filters.size()) {
  165. Variant arg = p_peer;
  166. const Variant *argv[1] = { &arg };
  167. for (Callable filter : visibility_filters) {
  168. Variant ret;
  169. Callable::CallError err;
  170. filter.callp(argv, 1, ret, err);
  171. ERR_FAIL_COND_V(err.error != Callable::CallError::CALL_OK || ret.get_type() != Variant::BOOL, false);
  172. if (!ret.operator bool()) {
  173. return false;
  174. }
  175. }
  176. }
  177. return peer_visibility.has(0) || peer_visibility.has(p_peer);
  178. }
  179. void MultiplayerSynchronizer::add_visibility_filter(Callable p_callback) {
  180. visibility_filters.insert(p_callback);
  181. _update_process();
  182. }
  183. void MultiplayerSynchronizer::remove_visibility_filter(Callable p_callback) {
  184. visibility_filters.erase(p_callback);
  185. _update_process();
  186. }
  187. void MultiplayerSynchronizer::set_visibility_for(int p_peer, bool p_visible) {
  188. if (peer_visibility.has(p_peer) == p_visible) {
  189. return;
  190. }
  191. if (p_visible) {
  192. peer_visibility.insert(p_peer);
  193. } else {
  194. peer_visibility.erase(p_peer);
  195. }
  196. update_visibility(p_peer);
  197. }
  198. bool MultiplayerSynchronizer::get_visibility_for(int p_peer) const {
  199. return peer_visibility.has(p_peer);
  200. }
  201. void MultiplayerSynchronizer::set_visibility_update_mode(VisibilityUpdateMode p_mode) {
  202. visibility_update_mode = p_mode;
  203. _update_process();
  204. }
  205. MultiplayerSynchronizer::VisibilityUpdateMode MultiplayerSynchronizer::get_visibility_update_mode() const {
  206. return visibility_update_mode;
  207. }
  208. void MultiplayerSynchronizer::_bind_methods() {
  209. ClassDB::bind_method(D_METHOD("set_root_path", "path"), &MultiplayerSynchronizer::set_root_path);
  210. ClassDB::bind_method(D_METHOD("get_root_path"), &MultiplayerSynchronizer::get_root_path);
  211. ClassDB::bind_method(D_METHOD("set_replication_interval", "milliseconds"), &MultiplayerSynchronizer::set_replication_interval);
  212. ClassDB::bind_method(D_METHOD("get_replication_interval"), &MultiplayerSynchronizer::get_replication_interval);
  213. ClassDB::bind_method(D_METHOD("set_replication_config", "config"), &MultiplayerSynchronizer::set_replication_config);
  214. ClassDB::bind_method(D_METHOD("get_replication_config"), &MultiplayerSynchronizer::get_replication_config);
  215. ClassDB::bind_method(D_METHOD("set_visibility_update_mode", "mode"), &MultiplayerSynchronizer::set_visibility_update_mode);
  216. ClassDB::bind_method(D_METHOD("get_visibility_update_mode"), &MultiplayerSynchronizer::get_visibility_update_mode);
  217. ClassDB::bind_method(D_METHOD("update_visibility", "for_peer"), &MultiplayerSynchronizer::update_visibility, DEFVAL(0));
  218. ClassDB::bind_method(D_METHOD("set_visibility_public", "visible"), &MultiplayerSynchronizer::set_visibility_public);
  219. ClassDB::bind_method(D_METHOD("is_visibility_public"), &MultiplayerSynchronizer::is_visibility_public);
  220. ClassDB::bind_method(D_METHOD("add_visibility_filter", "filter"), &MultiplayerSynchronizer::add_visibility_filter);
  221. ClassDB::bind_method(D_METHOD("remove_visibility_filter", "filter"), &MultiplayerSynchronizer::remove_visibility_filter);
  222. ClassDB::bind_method(D_METHOD("set_visibility_for", "peer", "visible"), &MultiplayerSynchronizer::set_visibility_for);
  223. ClassDB::bind_method(D_METHOD("get_visibility_for", "peer"), &MultiplayerSynchronizer::get_visibility_for);
  224. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_path"), "set_root_path", "get_root_path");
  225. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "replication_interval", PROPERTY_HINT_RANGE, "0,5,0.001,suffix:s"), "set_replication_interval", "get_replication_interval");
  226. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "replication_config", PROPERTY_HINT_RESOURCE_TYPE, "SceneReplicationConfig", PROPERTY_USAGE_NO_EDITOR), "set_replication_config", "get_replication_config");
  227. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_update_mode", PROPERTY_HINT_ENUM, "Idle,Physics,None"), "set_visibility_update_mode", "get_visibility_update_mode");
  228. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "public_visibility"), "set_visibility_public", "is_visibility_public");
  229. BIND_ENUM_CONSTANT(VISIBILITY_PROCESS_IDLE);
  230. BIND_ENUM_CONSTANT(VISIBILITY_PROCESS_PHYSICS);
  231. BIND_ENUM_CONSTANT(VISIBILITY_PROCESS_NONE);
  232. ADD_SIGNAL(MethodInfo("visibility_changed", PropertyInfo(Variant::INT, "for_peer")));
  233. }
  234. void MultiplayerSynchronizer::_notification(int p_what) {
  235. #ifdef TOOLS_ENABLED
  236. if (Engine::get_singleton()->is_editor_hint()) {
  237. return;
  238. }
  239. #endif
  240. if (root_path.is_empty()) {
  241. return;
  242. }
  243. switch (p_what) {
  244. case NOTIFICATION_ENTER_TREE: {
  245. _start();
  246. } break;
  247. case NOTIFICATION_EXIT_TREE: {
  248. _stop();
  249. } break;
  250. case NOTIFICATION_INTERNAL_PROCESS:
  251. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  252. update_visibility(0);
  253. } break;
  254. }
  255. }
  256. void MultiplayerSynchronizer::set_replication_interval(double p_interval) {
  257. ERR_FAIL_COND_MSG(p_interval < 0, "Interval must be greater or equal to 0 (where 0 means default)");
  258. interval_msec = uint64_t(p_interval * 1000);
  259. }
  260. double MultiplayerSynchronizer::get_replication_interval() const {
  261. return double(interval_msec) / 1000.0;
  262. }
  263. void MultiplayerSynchronizer::set_replication_config(Ref<SceneReplicationConfig> p_config) {
  264. replication_config = p_config;
  265. }
  266. Ref<SceneReplicationConfig> MultiplayerSynchronizer::get_replication_config() {
  267. return replication_config;
  268. }
  269. void MultiplayerSynchronizer::update_visibility(int p_for_peer) {
  270. #ifdef TOOLS_ENABLED
  271. if (Engine::get_singleton()->is_editor_hint()) {
  272. return;
  273. }
  274. #endif
  275. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  276. if (node && get_multiplayer()->has_multiplayer_peer() && is_multiplayer_authority()) {
  277. emit_signal(SNAME("visibility_changed"), p_for_peer);
  278. }
  279. }
  280. void MultiplayerSynchronizer::set_root_path(const NodePath &p_path) {
  281. _stop();
  282. root_path = p_path;
  283. _start();
  284. }
  285. NodePath MultiplayerSynchronizer::get_root_path() const {
  286. return root_path;
  287. }
  288. void MultiplayerSynchronizer::set_multiplayer_authority(int p_peer_id, bool p_recursive) {
  289. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  290. if (!node || get_multiplayer_authority() == p_peer_id) {
  291. Node::set_multiplayer_authority(p_peer_id, p_recursive);
  292. return;
  293. }
  294. get_multiplayer()->object_configuration_remove(node, this);
  295. Node::set_multiplayer_authority(p_peer_id, p_recursive);
  296. get_multiplayer()->object_configuration_add(node, this);
  297. }
  298. MultiplayerSynchronizer::MultiplayerSynchronizer() {
  299. // Publicly visible by default.
  300. peer_visibility.insert(0);
  301. }