1 /*
2  * Copyright (C) 2010-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #include "network/network_player_settings_backend.h"
21 
22 #include "ai/computer_player.h"
23 
set_player_state(PlayerSlot id,PlayerSettings::State state)24 void NetworkPlayerSettingsBackend::set_player_state(PlayerSlot id, PlayerSettings::State state) {
25 	if (id >= s->settings().players.size()) {
26 		return;
27 	}
28 	s->set_player_state(id, state);
29 }
30 
set_player_ai(PlayerSlot id,const std::string & name,bool random_ai)31 void NetworkPlayerSettingsBackend::set_player_ai(PlayerSlot id,
32                                                  const std::string& name,
33                                                  bool random_ai) {
34 	if (id >= s->settings().players.size()) {
35 		return;
36 	}
37 	if (random_ai) {
38 		const ComputerPlayer::ImplementationVector& impls = ComputerPlayer::get_implementations();
39 		ComputerPlayer::ImplementationVector::const_iterator it = impls.begin();
40 		if (impls.size() > 1) {
41 			do {
42 				size_t random = (std::rand() % impls.size());  // Choose a random AI
43 				it = impls.begin() + random;
44 			} while ((*it)->type == ComputerPlayer::Implementation::Type::kEmpty);
45 		}
46 		s->set_player_ai(id, (*it)->name, random_ai);
47 	} else {
48 		s->set_player_ai(id, name, random_ai);
49 	}
50 }
51 
set_player_tribe(PlayerSlot id,const std::string & tribename)52 void NetworkPlayerSettingsBackend::set_player_tribe(PlayerSlot id, const std::string& tribename) {
53 	const GameSettings& settings = s->settings();
54 	if (id >= settings.players.size() || tribename.empty()) {
55 		return;
56 	}
57 	if (settings.players.at(id).state != PlayerSettings::State::kShared) {
58 		s->set_player_tribe(id, tribename, tribename == "random");
59 	}
60 }
61 
62 /// Set the shared in player for the given id
set_player_shared(PlayerSlot id,Widelands::PlayerNumber shared)63 void NetworkPlayerSettingsBackend::set_player_shared(PlayerSlot id,
64                                                      Widelands::PlayerNumber shared) {
65 	const GameSettings& settings = s->settings();
66 	if (id >= settings.players.size() || shared > settings.players.size()) {
67 		return;
68 	}
69 	if (settings.players.at(id).state == PlayerSettings::State::kShared) {
70 		s->set_player_shared(id, shared);
71 	}
72 }
73 
74 /// Sets the initialization for the player slot (Headquarters, Fortified Village etc.)
set_player_init(PlayerSlot id,uint8_t initialization_index)75 void NetworkPlayerSettingsBackend::set_player_init(PlayerSlot id, uint8_t initialization_index) {
76 	if (id >= s->settings().players.size()) {
77 		return;
78 	}
79 	s->set_player_init(id, initialization_index);
80 }
81 
82 /// Sets the team for the player slot
set_player_team(PlayerSlot id,Widelands::TeamNumber team)83 void NetworkPlayerSettingsBackend::set_player_team(PlayerSlot id, Widelands::TeamNumber team) {
84 	if (id >= s->settings().players.size()) {
85 		return;
86 	}
87 	s->set_player_team(id, team);
88 }
89