Skip to content

Commit 9744bee

Browse files
committed
Refactored the mechanism to spawn Bots and Npcs.
1 parent d755626 commit 9744bee

File tree

4 files changed

+8
-114
lines changed

4 files changed

+8
-114
lines changed

core/peer_networked_controller.cpp

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -61,60 +61,6 @@ const std::vector<ObjectData *> &PeerNetworkedController::get_sorted_controllabl
6161
return _sorted_controllable_objects;
6262
}
6363

64-
void PeerNetworkedController::set_server_controlled(bool p_server_controlled) {
65-
if (server_controlled == p_server_controlled) {
66-
// It's the same, nothing to do.
67-
return;
68-
}
69-
70-
if (is_networking_initialized()) {
71-
if (is_server_controller()) {
72-
// This is the server, let's start the procedure to switch controll mode.
73-
74-
#ifdef DEBUG_ENABLED
75-
ASSERT_COND_MSG(scene_synchronizer, "When the `NetworkedController` is a server, the `scene_synchronizer` is always set.");
76-
#endif
77-
78-
// First update the variable.
79-
server_controlled = p_server_controlled;
80-
81-
// Notify the `SceneSynchronizer` about it.
82-
scene_synchronizer->notify_controller_control_mode_changed(this);
83-
84-
// Tell the client to do the switch too.
85-
if (authority_peer != 1) {
86-
scene_synchronizer->call_rpc_set_server_controlled(
87-
authority_peer,
88-
authority_peer,
89-
server_controlled);
90-
} else {
91-
SceneSynchronizerDebugger::singleton()->print(WARNING, "The peer_controller is owned by the server, there is no client that can control it; please assign the proper authority.", "CONTROLLER-" + std::to_string(authority_peer));
92-
}
93-
94-
} else if (is_player_controller() || is_doll_controller()) {
95-
SceneSynchronizerDebugger::singleton()->print(WARNING, "You should never call the function `set_server_controlled` on the client, this has an effect only if called on the server.", "CONTROLLER-" + std::to_string(authority_peer));
96-
97-
} else if (is_nonet_controller()) {
98-
// There is no networking, the same instance is both the client and the
99-
// server already, nothing to do.
100-
server_controlled = p_server_controlled;
101-
102-
} else {
103-
#ifdef DEBUG_ENABLED
104-
ASSERT_NO_ENTRY_MSG("Unreachable, all the cases are handled.");
105-
#endif
106-
}
107-
} else {
108-
// This called during initialization or on the editor, nothing special just
109-
// set it.
110-
server_controlled = p_server_controlled;
111-
}
112-
}
113-
114-
bool PeerNetworkedController::get_server_controlled() const {
115-
return server_controlled;
116-
}
117-
11864
void PeerNetworkedController::set_max_redundant_inputs(int p_max) {
11965
max_redundant_inputs = p_max;
12066
}
@@ -305,14 +251,6 @@ void PeerNetworkedController::notify_receive_inputs(const Vector<uint8_t> &p_dat
305251
}
306252
}
307253

308-
void PeerNetworkedController::notify_set_server_controlled(bool p_server_controlled) {
309-
ENSURE_MSG(is_player_controller(), "This function is supposed to be called on the server.");
310-
server_controlled = p_server_controlled;
311-
312-
ENSURE_MSG(scene_synchronizer, "The server controller is supposed to be set on the client at this point.");
313-
scene_synchronizer->notify_controller_control_mode_changed(this);
314-
}
315-
316254
void PeerNetworkedController::player_set_has_new_input(bool p_has) {
317255
has_player_new_input = p_has;
318256
}
@@ -793,7 +731,7 @@ AutonomousServerController::AutonomousServerController(
793731
}
794732

795733
bool AutonomousServerController::receive_inputs(const Vector<uint8_t> &p_data) {
796-
SceneSynchronizerDebugger::singleton()->print(WARNING, "`receive_input` called on the `AutonomousServerController` - If this is called just after `set_server_controlled(true)` is called, you can ignore this warning, as the client is not aware about the switch for a really small window after this function call.", "CONTROLLER-" + std::to_string(peer_controller->authority_peer));
734+
SceneSynchronizerDebugger::singleton()->print(ERROR, "`receive_input` called on the `AutonomousServerController` it should not happen by design. This is a bug.", "CONTROLLER-" + std::to_string(peer_controller->authority_peer));
797735
return false;
798736
}
799737

core/peer_networked_controller.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ class PeerNetworkedController final {
5858
};
5959

6060
private:
61-
/// When `true`, this controller is controlled by the server: All the clients
62-
/// see it as a `Doll`.
63-
/// This property is really useful to implement bots (Character controlled by
64-
/// the AI).
65-
///
66-
/// NOTICE: Generally you specify this property on the editor, in addition
67-
/// it's possible to change this at runtime: this will cause the server to
68-
/// notify all the clients; so the switch is not immediate. This feature can be
69-
/// used to switch the Character possession between the AI (Server) and
70-
/// PlayerController (Client) without the need to re-instantiate the Character.
71-
bool server_controlled = false;
72-
7361
/// Amount of time an inputs is re-sent to each peer.
7462
/// Resenging inputs is necessary because the packets may be lost since as
7563
/// they are sent in an unreliable way.
@@ -108,9 +96,6 @@ class PeerNetworkedController final {
10896

10997
const std::vector<ObjectData *> &get_sorted_controllable_objects();
11098

111-
void set_server_controlled(bool p_server_controlled);
112-
bool get_server_controlled() const;
113-
11499
void set_max_redundant_inputs(int p_max);
115100
int get_max_redundant_inputs() const;
116101

@@ -171,7 +156,6 @@ class PeerNetworkedController final {
171156
void controllable_process(double p_delta, DataBuffer &p_data_buffer);
172157

173158
void notify_receive_inputs(const Vector<uint8_t> &p_data);
174-
void notify_set_server_controlled(bool p_server_controlled);
175159

176160
private:
177161
void player_set_has_new_input(bool p_has);

scene_synchronizer.cpp

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,6 @@ void SceneSynchronizerBase::setup(SynchronizerManager &p_synchronizer_interface)
108108
false,
109109
false);
110110

111-
rpc_handle_set_server_controlled =
112-
network_interface->rpc_config(
113-
std::function<void(int, bool)>(std::bind(&SceneSynchronizerBase::rpc_set_server_controlled, this, std::placeholders::_1, std::placeholders::_2)),
114-
true,
115-
false);
116-
117111
clear();
118112
reset_synchronizer_mode();
119113

@@ -151,7 +145,6 @@ void SceneSynchronizerBase::conclude() {
151145
rpc_handler_trickled_sync_data.reset();
152146
rpc_handle_notify_netstats.reset();
153147
rpc_handle_receive_input.reset();
154-
rpc_handle_set_server_controlled.reset();
155148
}
156149

157150
void SceneSynchronizerBase::process(double p_delta) {
@@ -1129,12 +1122,6 @@ void SceneSynchronizerBase::clear() {
11291122
process_functions__clear();
11301123
}
11311124

1132-
void SceneSynchronizerBase::notify_controller_control_mode_changed(PeerNetworkedController *controller) {
1133-
if (controller) {
1134-
reset_controller(*controller);
1135-
}
1136-
}
1137-
11381125
void SceneSynchronizerBase::rpc_receive_state(DataBuffer &p_snapshot) {
11391126
ENSURE_MSG(is_client(), "Only clients are suposed to receive the server snapshot.");
11401127
static_cast<ClientSynchronizer *>(synchronizer)->receive_snapshot(p_snapshot);
@@ -1264,28 +1251,13 @@ void SceneSynchronizerBase::call_rpc_receive_inputs(int p_recipient, int p_peer,
12641251
p_data);
12651252
}
12661253

1267-
void SceneSynchronizerBase::call_rpc_set_server_controlled(int p_recipient, int p_peer, bool p_server_controlled) {
1268-
rpc_handle_set_server_controlled.rpc(
1269-
get_network_interface(),
1270-
p_recipient,
1271-
p_peer,
1272-
p_server_controlled);
1273-
}
1274-
12751254
void SceneSynchronizerBase::rpc_receive_inputs(int p_peer, const Vector<uint8_t> &p_data) {
12761255
PeerData *pd = MapFunc::get_or_null(peer_data, p_peer);
12771256
if (pd && pd->get_controller()) {
12781257
pd->get_controller()->notify_receive_inputs(p_data);
12791258
}
12801259
}
12811260

1282-
void SceneSynchronizerBase::rpc_set_server_controlled(int p_peer, bool p_server_controlled) {
1283-
PeerData *pd = MapFunc::get_or_null(peer_data, p_peer);
1284-
if (pd && pd->get_controller()) {
1285-
pd->get_controller()->notify_set_server_controlled(p_server_controlled);
1286-
}
1287-
}
1288-
12891261
void SceneSynchronizerBase::clear_peers() {
12901262
// Copy, so we can safely remove the peers from `peer_data`.
12911263
std::vector<int> peers_tmp;
@@ -1653,17 +1625,22 @@ void SceneSynchronizerBase::reset_controller(PeerNetworkedController &p_controll
16531625
if (!network_interface->is_local_peer_networked()) {
16541626
p_controller.controller_type = PeerNetworkedController::CONTROLLER_TYPE_NONETWORK;
16551627
p_controller.controller = memnew(NoNetController(&p_controller));
1628+
16561629
} else if (network_interface->is_local_peer_server()) {
1657-
if (p_controller.get_server_controlled()) {
1630+
if (p_controller.get_authority_peer() == get_network_interface().get_server_peer()) {
1631+
// This is the server controller that is used to control the BOTs / NPCs.
16581632
p_controller.controller_type = PeerNetworkedController::CONTROLLER_TYPE_AUTONOMOUS_SERVER;
16591633
p_controller.controller = memnew(AutonomousServerController(&p_controller));
1634+
16601635
} else {
16611636
p_controller.controller_type = PeerNetworkedController::CONTROLLER_TYPE_SERVER;
16621637
p_controller.controller = memnew(ServerController(&p_controller));
16631638
}
1664-
} else if (get_network_interface().fetch_local_peer_id() == p_controller.get_authority_peer() && p_controller.get_server_controlled() == false) {
1639+
1640+
} else if (get_network_interface().fetch_local_peer_id() == p_controller.get_authority_peer()) {
16651641
p_controller.controller_type = PeerNetworkedController::CONTROLLER_TYPE_PLAYER;
16661642
p_controller.controller = memnew(PlayerController(&p_controller));
1643+
16671644
} else {
16681645
p_controller.controller_type = PeerNetworkedController::CONTROLLER_TYPE_DOLL;
16691646
p_controller.controller = memnew(DollController(&p_controller));

scene_synchronizer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ class SceneSynchronizerBase {
171171

172172
// Controller RPCs.
173173
RpcHandle<int, const Vector<uint8_t> &> rpc_handle_receive_input;
174-
RpcHandle<int, bool> rpc_handle_set_server_controlled;
175174

176175
/// Fixed rate at which the NetSync has to produce frames.
177176
int frames_per_seconds = 60;
@@ -389,10 +388,8 @@ class SceneSynchronizerBase {
389388
void rpc_notify_netstats(DataBuffer &p_data);
390389

391390
void call_rpc_receive_inputs(int p_recipient, int p_peer, const Vector<uint8_t> &p_data);
392-
void call_rpc_set_server_controlled(int p_recipient, int p_peer, bool p_server_controlled);
393391

394392
void rpc_receive_inputs(int p_peer, const Vector<uint8_t> &p_data);
395-
void rpc_set_server_controlled(int p_peer, bool p_server_controlled);
396393

397394
public: // ---------------------------------------------------------------- APIs
398395
void set_settings(Settings &p_settings);
@@ -526,8 +523,6 @@ class SceneSynchronizerBase {
526523
void reset_synchronizer_mode();
527524
void clear();
528525

529-
void notify_controller_control_mode_changed(PeerNetworkedController *controller);
530-
531526
void clear_peers();
532527

533528
void detect_and_signal_changed_variables(int p_flags);

0 commit comments

Comments
 (0)