Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/multiplayer_synchronizer.h
10277 views
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
#pragma once
32
33
#include "scene_replication_config.h"
34
35
#include "scene/main/node.h"
36
37
class MultiplayerSynchronizer : public Node {
38
GDCLASS(MultiplayerSynchronizer, Node);
39
40
public:
41
enum VisibilityUpdateMode {
42
VISIBILITY_PROCESS_IDLE,
43
VISIBILITY_PROCESS_PHYSICS,
44
VISIBILITY_PROCESS_NONE,
45
};
46
47
private:
48
struct Watcher {
49
NodePath prop;
50
uint64_t last_change_usec = 0;
51
Variant value;
52
};
53
54
Ref<SceneReplicationConfig> replication_config;
55
NodePath root_path = NodePath(".."); // Start with parent, like with AnimationPlayer.
56
uint64_t sync_interval_usec = 0;
57
uint64_t delta_interval_usec = 0;
58
VisibilityUpdateMode visibility_update_mode = VISIBILITY_PROCESS_IDLE;
59
HashSet<Callable> visibility_filters;
60
HashSet<int> peer_visibility;
61
Vector<Watcher> watchers;
62
uint64_t last_watch_usec = 0;
63
64
ObjectID root_node_cache;
65
uint64_t last_sync_usec = 0;
66
uint16_t last_inbound_sync = 0;
67
uint32_t net_id = 0;
68
bool sync_started = false;
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
76
protected:
77
static void _bind_methods();
78
void _notification(int p_what);
79
80
public:
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
SceneReplicationConfig *get_replication_config_ptr() const;
122
123
MultiplayerSynchronizer();
124
};
125
126
VARIANT_ENUM_CAST(MultiplayerSynchronizer::VisibilityUpdateMode);
127
128