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
31#ifndef MULTIPLAYER_SYNCHRONIZER_H
32#define MULTIPLAYER_SYNCHRONIZER_H
33
34#include "scene_replication_config.h"
35
36#include "scene/main/node.h"
37
38class MultiplayerSynchronizer : public Node {
39 GDCLASS(MultiplayerSynchronizer, Node);
40
41public:
42 enum VisibilityUpdateMode {
43 VISIBILITY_PROCESS_IDLE,
44 VISIBILITY_PROCESS_PHYSICS,
45 VISIBILITY_PROCESS_NONE,
46 };
47
48private:
49 struct Watcher {
50 NodePath prop;
51 uint64_t last_change_usec = 0;
52 Variant value;
53 };
54
55 Ref<SceneReplicationConfig> replication_config;
56 NodePath root_path = NodePath(".."); // Start with parent, like with AnimationPlayer.
57 uint64_t sync_interval_usec = 0;
58 uint64_t delta_interval_usec = 0;
59 VisibilityUpdateMode visibility_update_mode = VISIBILITY_PROCESS_IDLE;
60 HashSet<Callable> visibility_filters;
61 HashSet<int> peer_visibility;
62 Vector<Watcher> watchers;
63 uint64_t last_watch_usec = 0;
64
65 ObjectID root_node_cache;
66 uint64_t last_sync_usec = 0;
67 uint16_t last_inbound_sync = 0;
68 uint32_t net_id = 0;
69
70 static Object *_get_prop_target(Object *p_obj, const NodePath &p_prop);
71 void _start();
72 void _stop();
73 void _update_process();
74 Error _watch_changes(uint64_t p_usec);
75
76protected:
77 static void _bind_methods();
78 void _notification(int p_what);
79
80public:
81 static Error get_state(const List<NodePath> &p_properties, Object *p_obj, Vector<Variant> &r_variant, Vector<const Variant *> &r_variant_ptrs);
82 static Error set_state(const List<NodePath> &p_properties, Object *p_obj, const Vector<Variant> &p_state);
83
84 void reset();
85 Node *get_root_node();
86
87 uint32_t get_net_id() const;
88 void set_net_id(uint32_t p_net_id);
89
90 bool update_outbound_sync_time(uint64_t p_usec);
91 bool update_inbound_sync_time(uint16_t p_network_time);
92
93 PackedStringArray get_configuration_warnings() const override;
94
95 void set_replication_interval(double p_interval);
96 double get_replication_interval() const;
97
98 void set_delta_interval(double p_interval);
99 double get_delta_interval() const;
100
101 void set_replication_config(Ref<SceneReplicationConfig> p_config);
102 Ref<SceneReplicationConfig> get_replication_config();
103
104 void set_root_path(const NodePath &p_path);
105 NodePath get_root_path() const;
106 virtual void set_multiplayer_authority(int p_peer_id, bool p_recursive = true) override;
107
108 bool is_visibility_public() const;
109 void set_visibility_public(bool p_public);
110 bool is_visible_to(int p_peer);
111 void set_visibility_for(int p_peer, bool p_visible);
112 bool get_visibility_for(int p_peer) const;
113 void update_visibility(int p_for_peer);
114 void set_visibility_update_mode(VisibilityUpdateMode p_mode);
115 void add_visibility_filter(Callable p_callback);
116 void remove_visibility_filter(Callable p_callback);
117 VisibilityUpdateMode get_visibility_update_mode() const;
118
119 List<Variant> get_delta_state(uint64_t p_cur_usec, uint64_t p_last_usec, uint64_t &r_indexes);
120 List<NodePath> get_delta_properties(uint64_t p_indexes);
121
122 MultiplayerSynchronizer();
123};
124
125VARIANT_ENUM_CAST(MultiplayerSynchronizer::VisibilityUpdateMode);
126
127#endif // MULTIPLAYER_SYNCHRONIZER_H
128