multiplayer_synchronizer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**************************************************************************/
  2. /* multiplayer_synchronizer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef MULTIPLAYER_SYNCHRONIZER_H
  31. #define MULTIPLAYER_SYNCHRONIZER_H
  32. #include "scene_replication_config.h"
  33. #include "scene/main/node.h"
  34. class MultiplayerSynchronizer : public Node {
  35. GDCLASS(MultiplayerSynchronizer, Node);
  36. public:
  37. enum VisibilityUpdateMode {
  38. VISIBILITY_PROCESS_IDLE,
  39. VISIBILITY_PROCESS_PHYSICS,
  40. VISIBILITY_PROCESS_NONE,
  41. };
  42. private:
  43. struct Watcher {
  44. NodePath prop;
  45. uint64_t last_change_usec = 0;
  46. Variant value;
  47. };
  48. Ref<SceneReplicationConfig> replication_config;
  49. NodePath root_path = NodePath(".."); // Start with parent, like with AnimationPlayer.
  50. uint64_t sync_interval_usec = 0;
  51. uint64_t delta_interval_usec = 0;
  52. VisibilityUpdateMode visibility_update_mode = VISIBILITY_PROCESS_IDLE;
  53. HashSet<Callable> visibility_filters;
  54. HashSet<int> peer_visibility;
  55. Vector<Watcher> watchers;
  56. uint64_t last_watch_usec = 0;
  57. ObjectID root_node_cache;
  58. uint64_t last_sync_usec = 0;
  59. uint16_t last_inbound_sync = 0;
  60. uint32_t net_id = 0;
  61. bool sync_started = false;
  62. static Object *_get_prop_target(Object *p_obj, const NodePath &p_prop);
  63. void _start();
  64. void _stop();
  65. void _update_process();
  66. Error _watch_changes(uint64_t p_usec);
  67. protected:
  68. static void _bind_methods();
  69. void _notification(int p_what);
  70. public:
  71. static Error get_state(const List<NodePath> &p_properties, Object *p_obj, Vector<Variant> &r_variant, Vector<const Variant *> &r_variant_ptrs);
  72. static Error set_state(const List<NodePath> &p_properties, Object *p_obj, const Vector<Variant> &p_state);
  73. void reset();
  74. Node *get_root_node();
  75. uint32_t get_net_id() const;
  76. void set_net_id(uint32_t p_net_id);
  77. bool update_outbound_sync_time(uint64_t p_usec);
  78. bool update_inbound_sync_time(uint16_t p_network_time);
  79. PackedStringArray get_configuration_warnings() const override;
  80. void set_replication_interval(double p_interval);
  81. double get_replication_interval() const;
  82. void set_delta_interval(double p_interval);
  83. double get_delta_interval() const;
  84. void set_replication_config(Ref<SceneReplicationConfig> p_config);
  85. Ref<SceneReplicationConfig> get_replication_config();
  86. void set_root_path(const NodePath &p_path);
  87. NodePath get_root_path() const;
  88. virtual void set_multiplayer_authority(int p_peer_id, bool p_recursive = true) override;
  89. bool is_visibility_public() const;
  90. void set_visibility_public(bool p_public);
  91. bool is_visible_to(int p_peer);
  92. void set_visibility_for(int p_peer, bool p_visible);
  93. bool get_visibility_for(int p_peer) const;
  94. void update_visibility(int p_for_peer);
  95. void set_visibility_update_mode(VisibilityUpdateMode p_mode);
  96. void add_visibility_filter(Callable p_callback);
  97. void remove_visibility_filter(Callable p_callback);
  98. VisibilityUpdateMode get_visibility_update_mode() const;
  99. List<Variant> get_delta_state(uint64_t p_cur_usec, uint64_t p_last_usec, uint64_t &r_indexes);
  100. List<NodePath> get_delta_properties(uint64_t p_indexes);
  101. SceneReplicationConfig *get_replication_config_ptr() const;
  102. MultiplayerSynchronizer();
  103. };
  104. VARIANT_ENUM_CAST(MultiplayerSynchronizer::VisibilityUpdateMode);
  105. #endif // MULTIPLAYER_SYNCHRONIZER_H