Skip to content

Commit

Permalink
Merge pull request #1068 from wxwisiasdf/setplayeronai
Browse files Browse the repository at this point in the history
set player on AI (with make_ai parameter for command too)
  • Loading branch information
schombert authored Feb 4, 2024
2 parents 1ddec1b + e06af93 commit fca7f1d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/entry_point_nix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ int main(int argc, char **argv) {
game_state.quit_signaled.store(true, std::memory_order_release);
update_thread.join();

network::finish(game_state);
return EXIT_SUCCESS;
}
18 changes: 10 additions & 8 deletions src/gamestate/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4285,19 +4285,21 @@ void execute_notify_player_joins(sys::state& state, dcon::nation_id source, sys:
ai::remove_ai_data(state, source);
}

void notify_player_leaves(sys::state& state, dcon::nation_id source) {
void notify_player_leaves(sys::state& state, dcon::nation_id source, bool make_ai) {
payload p;
memset(&p, 0, sizeof(payload));
p.type = command_type::notify_player_leaves;
p.source = source;
p.data.notify_leave.make_ai = make_ai;
add_to_command_queue(state, p);
}
bool can_notify_player_leaves(sys::state& state, dcon::nation_id source) {
// TODO: bans, kicks, mutes?
return true;
bool can_notify_player_leaves(sys::state& state, dcon::nation_id source, bool make_ai) {
return state.world.nation_get_is_player_controlled(source);
}
void execute_notify_player_leaves(sys::state& state, dcon::nation_id source) {
state.world.nation_set_is_player_controlled(source, false);
void execute_notify_player_leaves(sys::state& state, dcon::nation_id source, bool make_ai) {
if(make_ai) {
state.world.nation_set_is_player_controlled(source, false);
}

ui::chat_message m{};
m.source = source;
Expand Down Expand Up @@ -4877,7 +4879,7 @@ bool can_perform_command(sys::state& state, payload& c) {
return can_notify_player_joins(state, c.source, c.data.player_name);

case command_type::notify_player_leaves:
return can_notify_player_leaves(state, c.source);
return can_notify_player_leaves(state, c.source, c.data.notify_leave.make_ai);

case command_type::notify_player_picks_nation:
return can_notify_player_picks_nation(state, c.source, c.data.nation_pick.target);
Expand Down Expand Up @@ -5248,7 +5250,7 @@ void execute_command(sys::state& state, payload& c) {
execute_notify_player_joins(state, c.source, c.data.player_name);
break;
case command_type::notify_player_leaves:
execute_notify_player_leaves(state, c.source);
execute_notify_player_leaves(state, c.source, c.data.notify_leave.make_ai);
break;
case command_type::notify_player_picks_nation:
execute_notify_player_picks_nation(state, c.source, c.data.nation_pick.target);
Expand Down
8 changes: 6 additions & 2 deletions src/gamestate/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ struct notify_save_loaded_data {
struct notify_reload_data {
sys::checksum_key checksum;
};
struct notify_leaves_data {
bool make_ai;
};

struct payload {
union dtype {
Expand Down Expand Up @@ -515,6 +518,7 @@ struct payload {
notify_reload_data notify_reload;
sys::player_name player_name;
cheat_location_data cheat_location;
notify_leaves_data notify_leave;
cheat_invention_data_t cheat_invention_data;
dtype() { }
} data;
Expand Down Expand Up @@ -845,8 +849,8 @@ void notify_player_kick(sys::state& state, dcon::nation_id source, dcon::nation_
bool can_notify_player_kick(sys::state& state, dcon::nation_id source, dcon::nation_id target);
void notify_player_joins(sys::state& state, dcon::nation_id source, sys::player_name& name);
bool can_notify_player_joins(sys::state& state, dcon::nation_id source, sys::player_name& name);
void notify_player_leaves(sys::state& state, dcon::nation_id source);
bool can_notify_player_leaves(sys::state& state, dcon::nation_id source);
void notify_player_leaves(sys::state& state, dcon::nation_id source, bool make_ai);
bool can_notify_player_leaves(sys::state& state, dcon::nation_id source, bool make_ai);
void notify_player_picks_nation(sys::state& state, dcon::nation_id source, dcon::nation_id target);
bool can_notify_player_picks_nation(sys::state& state, dcon::nation_id source, dcon::nation_id target);
void notify_player_oos(sys::state& state, dcon::nation_id source);
Expand Down
51 changes: 42 additions & 9 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,10 @@ void init(sys::state& state) {
}
}

static void disconnect_client(sys::state& state, client_data& client) {
command::notify_player_leaves(state, client.playing_as);
static void disconnect_client(sys::state& state, client_data& client, bool graceful) {
if(command::can_notify_player_leaves(state, client.playing_as, graceful)) {
command::notify_player_leaves(state, client.playing_as, graceful);
}
socket_shutdown(client.socket_fd);
client.socket_fd = 0;
client.send_buffer.clear();
Expand Down Expand Up @@ -551,7 +553,7 @@ static void receive_from_clients(sys::state& state) {
if(client.handshake) {
r = socket_recv(client.socket_fd, &client.hshake_buffer, sizeof(client.hshake_buffer), &client.recv_count, [&]() {
if(std::memcmp(client.hshake_buffer.password, state.network_state.password, sizeof(state.network_state.password)) != 0) {
disconnect_client(state, client);
disconnect_client(state, client, false);
return;
}
send_post_handshake_commands(state, client);
Expand All @@ -572,7 +574,6 @@ static void receive_from_clients(sys::state& state) {
case command::command_type::notify_stop_game:
case command::command_type::notify_pause_game:
case command::command_type::notify_player_joins:
case command::command_type::notify_player_leaves:
case command::command_type::save_game:
break; // has to be valid/sendable by client
default:
Expand All @@ -593,7 +594,7 @@ static void receive_from_clients(sys::state& state) {
#if !defined(NDEBUG) && defined(_WIN32)
state.console_log("host:disconnect: in-receive err=" + std::to_string(int32_t(r)) + "::" + get_wsa_error_text(WSAGetLastError()));
#endif
network::disconnect_client(state, client);
network::disconnect_client(state, client, false);
}
}
}
Expand Down Expand Up @@ -672,11 +673,11 @@ static void accept_new_clients(sys::state& state) {
client.socket_fd = accept(state.network_state.socket_fd, (struct sockaddr*)&client.v4_address, &addr_len);
}
if(client.is_banned(state)) {
disconnect_client(state, client);
disconnect_client(state, client, false);
break;
}
if(state.mode == sys::game_mode_type::end_screen) {
disconnect_client(state, client);
disconnect_client(state, client, false);
break;
}
/* Send it data so she is in sync with everyone else! */
Expand Down Expand Up @@ -748,7 +749,7 @@ void send_and_receive_commands(sys::state& state) {
#if !defined(NDEBUG) && defined(_WIN32)
state.console_log("host:disconnect: in-send-EARLY err=" + std::to_string(int32_t(r)) + "::" + get_wsa_error_text(WSAGetLastError()));
#endif
disconnect_client(state, client);
disconnect_client(state, client, false);
continue;
}
client.total_sent_bytes += old_size - client.early_send_buffer.size();
Expand All @@ -765,7 +766,7 @@ void send_and_receive_commands(sys::state& state) {
#if !defined(NDEBUG) && defined(_WIN32)
state.console_log("host:disconnect: in-send-INGAME err=" + std::to_string(int32_t(r)) + "::" + get_wsa_error_text(WSAGetLastError()));
#endif
disconnect_client(state, client);
disconnect_client(state, client, false);
continue;
}
client.total_sent_bytes += old_size - client.send_buffer.size();
Expand Down Expand Up @@ -933,6 +934,38 @@ void send_and_receive_commands(sys::state& state) {
void finish(sys::state& state) {
if(state.network_mode == sys::network_mode_type::single_player)
return; // Do nothing in singleplayer

if(state.network_mode == sys::network_mode_type::client) {
if(!state.network_state.save_stream) {
// send the outgoing commands to the server and flush the entire queue
{
auto* c = state.network_state.outgoing_commands.front();
while(c) {
if(c->type == command::command_type::save_game) {
command::execute_command(state, *c);
} else {
socket_add_to_send_queue(state.network_state.send_buffer, c, sizeof(*c));
}
state.network_state.outgoing_commands.pop();
c = state.network_state.outgoing_commands.front();
}
}
command::payload c;
memset(&c, 0, sizeof(c));
c.type = command::command_type::notify_player_leaves;
c.source = state.local_player_nation;
c.data.notify_leave.make_ai = true;
socket_add_to_send_queue(state.network_state.send_buffer, &c, sizeof(c));
while(state.network_state.send_buffer.size() > 0) {
if(socket_send(state.network_state.socket_fd, state.network_state.send_buffer) != 0) { // error
#ifdef _WIN64
MessageBoxA(NULL, ("Network client command send error: " + get_wsa_error_text(WSAGetLastError())).c_str(), "Network error", MB_OK);
#endif
std::abort();
}
}
}
}

socket_shutdown(state.network_state.socket_fd);
#ifdef _WIN64
Expand Down

0 comments on commit fca7f1d

Please sign in to comment.