-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
connect_engine.cpp
1371 lines (1140 loc) · 37.9 KB
/
connect_engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2013 - 2016 by Andrius Silinskas <silinskas.andrius@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "connect_engine.hpp"
#include "ai/configuration.hpp"
#include "config_assign.hpp"
#include "formula/string_utils.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "map/map.hpp"
#include "multiplayer_ui.hpp"
#include "mp_game_utils.hpp"
#include "mt_rng.hpp"
#include "playcampaign.hpp"
#include "tod_manager.hpp"
#include "multiplayer_ui.hpp" // For get_color_string
#include <boost/assign.hpp>
#include <stdlib.h>
#include <ctime>
static lg::log_domain log_config("config");
#define LOG_CF LOG_STREAM(info, log_config)
#define WRN_CF LOG_STREAM(warn, log_config)
#define ERR_CF LOG_STREAM(err, log_config)
static lg::log_domain log_mp_connect_engine("mp/connect/engine");
#define DBG_MP LOG_STREAM(debug, log_mp_connect_engine)
#define LOG_MP LOG_STREAM(info, log_mp_connect_engine)
#define WRN_MP LOG_STREAM(warn, log_mp_connect_engine)
#define ERR_MP LOG_STREAM(err, log_mp_connect_engine)
static lg::log_domain log_network("network");
#define LOG_NW LOG_STREAM(info, log_network)
namespace {
const std::string controller_names[] = {
"human",
"human",
"ai",
"null",
"reserved"
};
const std::string attributes_to_trim[] = {
"side",
"type",
"gender",
"recruit",
"player_id",
"previous_recruits",
"controller",
"current_player",
"team_name",
"user_team_name",
"color",
"gold",
"income",
"allow_changes"
};
}
namespace ng {
connect_engine::connect_engine(saved_game& state,
const bool first_scenario, mp_campaign_info* campaign_info) :
level_(),
state_(state),
params_(state.mp_settings()),
default_controller_(campaign_info ? CNTR_NETWORK : CNTR_LOCAL),
campaign_info_(campaign_info),
first_scenario_(first_scenario),
force_lock_settings_(),
side_engines_(),
era_factions_(),
team_names_(),
user_team_names_(),
player_teams_()
{
// Initial level config from the mp_game_settings.
level_ = mp::initial_level_config(state_);
if (level_.empty()) {
return;
}
const bool is_mp = state_.classification().is_normal_mp_game();
force_lock_settings_ = (!state.mp_settings().saved_game) && scenario()["force_lock_settings"].to_bool(!is_mp);
// Original level sides.
config::child_itors sides = current_config()->child_range("side");
// AI algorithms.
ai::configuration::add_era_ai_from_config(level_.child("era"));
ai::configuration::add_mod_ai_from_config(
level_.child_range("modification"));
// Set the team name lists and modify the original level sides,
// if necessary.
std::vector<std::string> original_team_names;
std::string team_prefix(std::string(_("Team")) + " ");
int side_count = 1;
for (config& side : sides) {
const std::string side_str = std::to_string(side_count);
config::attribute_value& team_name = side["team_name"];
config::attribute_value& user_team_name =
side["user_team_name"];
// Revert to default values if appropriate.
if (team_name.empty()) {
team_name = side_str;
}
if (params_.use_map_settings && user_team_name.empty()) {
user_team_name = team_name;
}
bool add_team = true;
if (params_.use_map_settings) {
// Only add a team if it is not found.
bool found = std::find(team_names_.begin(), team_names_.end(),
team_name.str()) != team_names_.end();
if (found) {
add_team = false;
}
} else {
// Always add a new team for every side, but leave
// the specified team assigned to a side if there is one.
std::vector<std::string>::const_iterator name_itor =
std::find(original_team_names.begin(),
original_team_names.end(), team_name.str());
if (name_itor == original_team_names.end()) {
original_team_names.push_back(team_name);
team_name = "Team " +
std::to_string(original_team_names.size());
} else {
team_name = "Team " + std::to_string(
name_itor - original_team_names.begin() + 1);
} // Note that the prefix "Team " is untranslatable, as team_name is not meant to be translated. This is needed so that the attribute
// is not interpretted as an int when reading from config, which causes bugs later.
user_team_name = team_prefix + side_str;
}
if (add_team) {
team_names_.push_back(params_.use_map_settings ? team_name :
"Team " + side_str);
user_team_names_.push_back(user_team_name.t_str().to_serialized());
if (side["allow_player"].to_bool(true) || game_config::debug) {
player_teams_.push_back(user_team_name.str());
}
}
++side_count;
}
// Selected era's factions.
for (const config& era : level_.child("era").child_range("multiplayer_side")) {
era_factions_.push_back(&era);
}
game_config::add_color_info(scenario());
// Create side engines.
int index = 0;
for (const config &s : sides) {
side_engine_ptr new_side_engine(new side_engine(s, *this, index));
side_engines_.push_back(new_side_engine);
index++;
}
if(first_scenario_) {
// Add host to the connected users list.
import_user(preferences::login(), false);
}
else {
// Add host but don't assign a side to him.
import_user(preferences::login(), true);
// Load reserved players information into the sides.
load_previous_sides_users();
}
//actualy only updates the sides in the level.
update_level();
// If we are connected, send data to the connected host.
send_level_data(0);
}
connect_engine::~connect_engine()
{
}
config* connect_engine::current_config() {
if(config& s = scenario())
return &s;
else
return nullptr;
}
void connect_engine::import_user(const std::string& name, const bool observer,
int side_taken)
{
config user_data;
user_data["name"] = name;
import_user(user_data, observer, side_taken);
}
void connect_engine::import_user(const config& data, const bool observer,
int side_taken)
{
const std::string& username = data["name"];
assert(!username.empty());
if(campaign_info_) {
connected_users_rw().insert(username);
}
update_side_controller_options();
if (observer) {
return;
}
bool side_assigned = false;
if (side_taken >= 0) {
side_engines_[side_taken]->place_user(data, true);
side_assigned = true;
}
// Check if user has a side(s) reserved for him.
for (side_engine_ptr side : side_engines_) {
if (side->reserved_for() == username && side->player_id().empty() && side->controller() != CNTR_COMPUTER) {
side->place_user(data);
side_assigned = true;
}
}
// If no sides were assigned for a user,
// take a first available side.
if (side_taken < 0 && !side_assigned) {
for (side_engine_ptr side : side_engines_) {
if (side->available_for_user(username) ||
side->controller() == CNTR_LOCAL) {
side->place_user(data);
side_assigned = true;
break;
}
}
}
// Check if user has taken any sides, which should get control
// over any other sides.
for (side_engine_ptr user_side : side_engines_) {
if(user_side->player_id() == username && !user_side->previous_save_id().empty()) {
for (side_engine_ptr side : side_engines_){
if(side->player_id().empty() && side->previous_save_id() == user_side->previous_save_id()) {
side->place_user(data);
}
}
}
}
}
bool connect_engine::sides_available() const
{
for (side_engine_ptr side : side_engines_) {
if (side->available_for_user()) {
return true;
}
}
return false;
}
void connect_engine::update_level()
{
DBG_MP << "updating level" << std::endl;
scenario().clear_children("side");
for (side_engine_ptr side : side_engines_) {
scenario().add_child("side", side->new_config());
}
}
void connect_engine::update_and_send_diff(bool /*update_time_of_day*/)
{
config old_level = level_;
update_level();
config diff = level_.get_diff(old_level);
if (!diff.empty()) {
config scenario_diff;
scenario_diff.add_child("scenario_diff", diff);
network::send_data(scenario_diff, 0);
}
}
bool connect_engine::can_start_game() const
{
if (side_engines_.size() == 0) {
return true;
}
// First check if all sides are ready to start the game.
for (side_engine_ptr side : side_engines_) {
if (!side->ready_for_start()) {
const int side_num = side->index() + 1;
DBG_MP << "not all sides are ready, side " <<
side_num << " not ready\n";
return false;
}
}
DBG_MP << "all sides are ready" << std::endl;
/*
* If at least one human player is slotted with a player/ai we're allowed
* to start. Before used a more advanced test but it seems people are
* creative in what is used in multiplayer [1] so use a simpler test now.
* [1] http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=568029
*/
for (side_engine_ptr side : side_engines_) {
if (side->controller() != CNTR_EMPTY && side->allow_player()) {
return true;
}
}
return false;
}
std::vector<std::string> side_engine::get_children_to_swap()
{
std::vector<std::string> children;
children.push_back("village");
children.push_back("unit");
children.push_back("ai");
return children;
}
std::multimap<std::string, config> side_engine::get_side_children()
{
std::multimap<std::string, config> children;
for (const std::string& children_to_swap : get_children_to_swap()) {
for (const config& child : cfg_.child_range(children_to_swap)) {
children.insert(std::pair<std::string, config>(children_to_swap, child));
}
}
return children;
}
void side_engine::set_side_children(std::multimap<std::string, config> children)
{
for (const std::string& children_to_remove : get_children_to_swap()) {
cfg_.clear_children(children_to_remove);
}
for (std::pair<std::string, config> child_map : children) {
cfg_.add_child(child_map.first, child_map.second);
}
}
void connect_engine::start_game()
{
DBG_MP << "starting a new game" << std::endl;
// Resolves the "random faction", "random gender" and "random message"
// Must be done before shuffle sides, or some cases will cause errors
rand_rng::mt_rng rng; // Make an RNG for all the shuffling and random faction operations
for (side_engine_ptr side : side_engines_) {
std::vector<std::string> avoid_faction_ids;
// If we aren't resolving random factions independently at random, calculate which factions should not appear for this side.
if (params_.random_faction_mode != mp_game_settings::RANDOM_FACTION_MODE::DEFAULT) {
for (side_engine_ptr side2 : side_engines_) {
if (!side2->flg().is_random_faction()) {
switch(params_.random_faction_mode.v) {
case mp_game_settings::RANDOM_FACTION_MODE::NO_MIRROR:
avoid_faction_ids.push_back(side2->flg().current_faction()["id"].str());
break;
case mp_game_settings::RANDOM_FACTION_MODE::NO_ALLY_MIRROR:
if (side2->team() == side->team()) {// TODO: When the connect engines are fixed to allow multiple teams, this should be changed to "if side1 and side2 are allied, i.e. their list of teams has nonempty intersection"
avoid_faction_ids.push_back(side2->flg().current_faction()["id"].str());
}
break;
default:
break; // assert(false);
}
}
}
}
side->resolve_random(rng, avoid_faction_ids);
}
// Shuffle sides (check settings and if it is a re-loaded game).
// Must be done after resolve_random() or shuffle sides, or they won't work.
if (state_.mp_settings().shuffle_sides && !force_lock_settings_ && !(level_.child("snapshot") && level_.child("snapshot").child("side"))) {
// Only playable sides should be shuffled.
std::vector<int> playable_sides;
for (side_engine_ptr side : side_engines_) {
if (side->allow_player() && side->allow_shuffle()) {
playable_sides.push_back(side->index());
}
}
// Fisher-Yates shuffle.
for (int i = playable_sides.size(); i > 1; i--)
{
int j_side = playable_sides[rng.get_next_random() % i];
int i_side = playable_sides[i - 1];
if (i_side == j_side) continue; //nothing to swap
// First we swap everything about a side with another
side_engine_ptr tmp_side = side_engines_[j_side];
side_engines_[j_side] = side_engines_[i_side];
side_engines_[i_side] = tmp_side;
// Some 'child' variables such as village ownership and
// initial side units need to be swapped over as well
std::multimap<std::string, config> tmp_side_children = side_engines_[j_side]->get_side_children();
side_engines_[j_side]->set_side_children(side_engines_[i_side]->get_side_children());
side_engines_[i_side]->set_side_children(tmp_side_children);
// Then we revert the swap for fields that are unique to
// player control and the team they selected
int tmp_index = side_engines_[j_side]->index();
side_engines_[j_side]->set_index(side_engines_[i_side]->index());
side_engines_[i_side]->set_index(tmp_index);
int tmp_team = side_engines_[j_side]->team();
side_engines_[j_side]->set_team(side_engines_[i_side]->team());
side_engines_[i_side]->set_team(tmp_team);
}
}
// Make other clients not show the results of resolve_random().
config lock("stop_updates");
network::send_data(lock, 0);
update_and_send_diff(true);
save_reserved_sides_information();
// Build the gamestate object after updating the level.
mp::level_to_gamestate(level_, state_);
network::send_data(config("start_game"), 0);
}
void connect_engine::start_game_commandline(
const commandline_options& cmdline_opts)
{
DBG_MP << "starting a new game in commandline mode" << std::endl;
typedef boost::tuple<unsigned int, std::string> mp_option;
rand_rng::mt_rng rng;
unsigned num = 0;
for (side_engine_ptr side : side_engines_) {
num++;
// Set the faction, if commandline option is given.
if (cmdline_opts.multiplayer_side) {
for (const mp_option& option : *cmdline_opts.multiplayer_side) {
if (option.get<0>() == num) {
if (std::find_if(era_factions_.begin(), era_factions_.end(), [&option](const config* faction) { return (*faction)["id"] == option.get<1>(); }) != era_factions_.end()) {
DBG_MP << "\tsetting side " << option.get<0>() << "\tfaction: " << option.get<1>() << std::endl;
side->set_faction_commandline(option.get<1>());
}
else {
ERR_MP << "failed to set side " << option.get<0>() << " to faction " << option.get<1>() << std::endl;
}
}
}
}
// Set the controller, if commandline option is given.
if (cmdline_opts.multiplayer_controller) {
for (const mp_option& option : *cmdline_opts.multiplayer_controller) {
if (option.get<0>() == num) {
DBG_MP << "\tsetting side " << option.get<0>() <<
"\tfaction: " << option.get<1>() << std::endl;
side->set_controller_commandline(option.get<1>());
}
}
}
// Set AI algorithm to RCA AI for all sides,
// then override if commandline option was given.
side->set_ai_algorithm("ai_default_rca");
if (cmdline_opts.multiplayer_algorithm) {
for (const mp_option& option : *cmdline_opts.multiplayer_algorithm) {
if (option.get<0>() == num) {
DBG_MP << "\tsetting side " << option.get<0>() <<
"\tfaction: " << option.get<1>() << std::endl;
side->set_ai_algorithm(option.get<1>());
}
}
}
// Finally, resolve "random faction",
// "random gender" and "random message", if any remains unresolved.
side->resolve_random(rng);
} // end top-level loop
update_and_send_diff(true);
// Update sides with commandline parameters.
if (cmdline_opts.multiplayer_turns) {
DBG_MP << "\tsetting turns: " << *cmdline_opts.multiplayer_turns <<
std::endl;
scenario()["turns"] = *cmdline_opts.multiplayer_turns;
}
for (config &side : scenario().child_range("side"))
{
if (cmdline_opts.multiplayer_ai_config) {
for (const mp_option& option : *cmdline_opts.multiplayer_ai_config) {
if (option.get<0>() == side["side"].to_unsigned()) {
DBG_MP << "\tsetting side " << side["side"] <<
"\tai_config: " << option.get<1>() << std::endl;
side["ai_config"] = option.get<1>();
}
}
}
// Having hard-coded values here is undesirable,
// but that's how it is done in the MP lobby
// part of the code also.
// Should be replaced by settings/constants in both places
if (cmdline_opts.multiplayer_ignore_map_settings) {
side["gold"] = 100;
side["income"] = 1;
}
typedef boost::tuple<unsigned int, std::string, std::string>
mp_parameter;
if (cmdline_opts.multiplayer_parm) {
for (const mp_parameter& parameter : *cmdline_opts.multiplayer_parm) {
if (parameter.get<0>() == side["side"].to_unsigned()) {
DBG_MP << "\tsetting side " << side["side"] << " " <<
parameter.get<1>() << ": " << parameter.get<2>() <<
std::endl;
side[parameter.get<1>()] = parameter.get<2>();
}
}
}
}
save_reserved_sides_information();
// Build the gamestate object after updating the level
mp::level_to_gamestate(level_, state_);
network::send_data(config("start_game"), 0);
}
std::pair<bool, bool> connect_engine::process_network_data(const config& data,
const network::connection sock)
{
std::pair<bool, bool> result(std::make_pair(false, true));
if (data.child("leave_game")) {
result.first = true;
return result;
}
// A side has been dropped.
if (!data["side_drop"].empty()) {
unsigned side_drop = data["side_drop"].to_int() - 1;
if (side_drop < side_engines_.size()) {
side_engine_ptr side_to_drop = side_engines_[side_drop];
// Remove user, whose side was dropped.
connected_users_rw().erase(side_to_drop->player_id());
update_side_controller_options();
side_to_drop->reset();
update_and_send_diff();
return result;
}
}
// A player is connecting to the game.
if (!data["side"].empty()) {
unsigned side_taken = data["side"].to_int() - 1;
// Checks if the connecting user has a valid and unique name.
const std::string name = data["name"];
if (name.empty()) {
config response;
response["failed"] = true;
network::send_data(response, sock);
ERR_CF << "ERROR: No username provided with the side." << std::endl;
return result;
}
if (connected_users().find(name) != connected_users().end()) {
// TODO: Seems like a needless limitation
// to only allow one side per player.
if (find_user_side_index_by_id(name) != -1) {
config response;
response["failed"] = true;
response["message"] = "The nickname '" + name +
"' is already in use.";
network::send_data(response, sock);
return result;
} else {
connected_users_rw().erase(name);
update_side_controller_options();
config observer_quit;
observer_quit.add_child("observer_quit")["name"] = name;
network::send_data(observer_quit, 0);
}
}
// Assigns this user to a side.
if (side_taken < side_engines_.size()) {
if (!side_engines_[side_taken]->available_for_user(name)) {
// This side is already taken.
// Try to reassing the player to a different position.
side_taken = 0;
for (side_engine_ptr s : side_engines_) {
if (s->available_for_user()) {
break;
}
side_taken++;
}
if (side_taken >= side_engines_.size()) {
config response;
response["failed"] = true;
network::send_data(response, sock);
config res;
config& kick = res.add_child("kick");
kick["username"] = data["name"];
network::send_data(res, 0);
update_and_send_diff();
ERR_CF << "ERROR: Couldn't assign a side to '" <<
name << "'\n";
return result;
}
}
LOG_CF << "client has taken a valid position\n";
import_user(data, false, side_taken);
update_and_send_diff();
// Wait for them to choose faction if allowed.
side_engines_[side_taken]->
set_waiting_to_choose_status(side_engines_[side_taken]->
allow_changes());
LOG_MP << "waiting to choose status = " <<
side_engines_[side_taken]->allow_changes() << std::endl;
result.second = false;
LOG_NW << "sent player data\n";
} else {
ERR_CF << "tried to take illegal side: " << side_taken << std::endl;
config response;
response["failed"] = true;
network::send_data(response, sock);
}
}
if (const config& change_faction = data.child("change_faction")) {
int side_taken = find_user_side_index_by_id(change_faction["name"]);
if (side_taken != -1 || !first_scenario_) {
import_user(change_faction, false, side_taken);
update_and_send_diff();
}
}
if (const config& observer = data.child("observer")) {
import_user(observer, true);
update_and_send_diff();
}
if (const config& observer = data.child("observer_quit")) {
const t_string& observer_name = observer["name"];
if (!observer_name.empty()) {
if ((connected_users().find(observer_name) != connected_users().end()) &&
(find_user_side_index_by_id(observer_name) != -1)) {
connected_users_rw().erase(observer_name);
update_side_controller_options();
update_and_send_diff();
}
}
}
return result;
}
void connect_engine::process_network_error(network::error& error)
{
// The problem isn't related to any specific connection and
// it's a general error. So we should just re-throw the error.
error.disconnect();
throw network::error(error.message);
}
int connect_engine::find_user_side_index_by_id(const std::string& id) const
{
size_t i = 0;
for (side_engine_ptr side : side_engines_) {
if (side->player_id() == id) {
break;
}
i++;
}
if (i >= side_engines_.size()) {
return -1;
}
return i;
}
void connect_engine::send_level_data(const network::connection sock) const
{
// Send initial information.
if (first_scenario_) {
network::send_data(config_of
("create_game", config_of
("name", params_.name)
("password", params_.password)
)
);
network::send_data(level_, sock);
} else {
network::send_data(config_of("update_game", config()), 0);
config next_level;
next_level.add_child("store_next_scenario", level_);
network::send_data(next_level, sock);
}
}
void connect_engine::save_reserved_sides_information()
{
// Add information about reserved sides to the level config.
// N.B. This information is needed only for a host player.
std::map<std::string, std::string> side_users =
utils::map_split(level_.child_or_empty("multiplayer")["side_users"]);
for (side_engine_ptr side : side_engines_) {
const std::string& save_id = side->save_id();
const std::string& player_id = side->player_id();
if (!save_id.empty() && !player_id.empty()) {
side_users[save_id] = player_id;
}
}
level_.child("multiplayer")["side_users"] = utils::join_map(side_users);
}
void connect_engine::load_previous_sides_users()
{
std::map<std::string, std::string> side_users =
utils::map_split(level_.child("multiplayer")["side_users"]);
std::set<std::string> names;
for (side_engine_ptr side : side_engines_) {
const std::string& save_id = side->previous_save_id();
if (side_users.find(save_id) != side_users.end()) {
side->set_reserved_for(side_users[save_id]);
if (side->controller() != CNTR_COMPUTER) {
side->set_controller(CNTR_RESERVED);
names.insert(side_users[save_id]);
}
side->update_controller_options();
}
}
//Do this in an extra loop to make sure we import each user only once.
for (const std::string& name : names)
{
if (connected_users().find(name) != connected_users().end() || !campaign_info_) {
import_user(name, false);
}
}
}
void connect_engine::update_side_controller_options()
{
for (side_engine_ptr side : side_engines_) {
side->update_controller_options();
}
}
const std::set<std::string>& connect_engine::connected_users() const
{
if(campaign_info_) {
return campaign_info_->connected_players;
}
else {
static std::set<std::string> empty;
return empty;
}
}
std::set<std::string>& connect_engine::connected_users_rw()
{
assert(campaign_info_);
return campaign_info_->connected_players;
}
side_engine::side_engine(const config& cfg, connect_engine& parent_engine,
const int index) :
cfg_(cfg),
parent_(parent_engine),
controller_(CNTR_NETWORK),
current_controller_index_(0),
controller_options_(),
allow_player_(cfg["allow_player"].to_bool(true)),
controller_lock_(cfg["controller_lock"].to_bool(
parent_.force_lock_settings_) && parent_.params_.use_map_settings),
index_(index),
team_(0),
color_(std::min(index, gamemap::MAX_PLAYERS - 1)),
gold_(cfg["gold"].to_int(100)),
income_(cfg["income"]),
reserved_for_(cfg["current_player"]),
player_id_(),
ai_algorithm_(),
chose_random_(cfg["chose_random"].to_bool(false)),
disallow_shuffle_(cfg["disallow_shuffle"].to_bool(false)),
flg_(parent_.era_factions_, cfg_, parent_.force_lock_settings_,
parent_.params_.use_map_settings, parent_.params_.saved_game),
allow_changes_(!parent_.params_.saved_game && !(flg_.choosable_factions().size() == 1 && flg_.choosable_leaders().size() == 1 && flg_.choosable_genders().size() == 1)),
waiting_to_choose_faction_(allow_changes_),
custom_color_()
{
// Check if this side should give its control to some other side.
const size_t side_cntr_index = cfg_["controller"].to_int(-1) - 1;
if (side_cntr_index < parent_.side_engines().size()) {
// Remove this attribute to avoid locking side
// to non-existing controller type.
cfg_.remove_attribute("controller");
cfg_["previous_save_id"] = parent_.side_engines()[side_cntr_index]->previous_save_id();
ERR_MP << "contoller=<number> is deperecated\n";
}
if(!parent_.params_.saved_game && cfg_["save_id"].str().empty()) {
assert(cfg_["id"].empty()); // we already setted "save_id" to "id" if "id" existed.
cfg_["save_id"] = parent_.scenario()["id"].str() + "_" + std::to_string(index);
}
update_controller_options();
// Tweak the controllers.
if (parent_.state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::SCENARIO && cfg_["controller"].blank())
{
cfg_["controller"] = "ai";
}
if(cfg_["controller"] != "human" && cfg_["controller"] != "ai" && cfg_["controller"] != "null") {
//an invalid contoller type was specified. Remove it to prevent asertion failures later.
cfg_.remove_attribute("controller");
}
if (cfg_["controller"] == "null") {
set_controller(CNTR_EMPTY);
} else if (cfg_["controller"] == "ai") {
set_controller(CNTR_COMPUTER);
} else if (parent_.default_controller_ == CNTR_NETWORK && !reserved_for_.empty()) {
// Reserve a side for "current_player", unless the side
// is played by an AI.
set_controller(CNTR_RESERVED);
} else if (allow_player_) {
set_controller(parent_.default_controller_);
} else {
// AI is the default.
set_controller(CNTR_COMPUTER);
}
// Initialize team and color.
unsigned team_name_index = 0;
for (const std::string& name : parent_.team_names_) {
if (name == cfg["team_name"]) {
break;
}
team_name_index++;
}
if (team_name_index >= parent_.team_names_.size()) {
assert(!parent_.team_names_.empty());
team_ = 0;
WRN_MP << "In side_engine constructor: Could not find my team_name " << cfg["team_name"] << " among the mp connect engine's list of team names. I am being assigned to the first team. This may indicate a bug!" << std::endl;
} else {
team_ = team_name_index;
}
if (!cfg["color"].empty()) {
if(cfg["color"].to_int()) {
color_ = cfg["color"].to_int() - 1;
}
else {
custom_color_ = cfg["color"].str();
color_ = 0;
}
}
// Initialize ai algorithm.
if (const config& ai = cfg.child("ai")) {
ai_algorithm_ = ai["ai_algorithm"].str();
}
}
side_engine::~side_engine()
{
}
std::string side_engine::user_description() const
{
switch(controller_)
{
case CNTR_LOCAL:
return N_("Anonymous player");
case CNTR_COMPUTER:
if (allow_player_) {
return ai::configuration::get_ai_config_for(ai_algorithm_)["description"];
} else {
return N_("Computer Player");
}
default:
return "";
}
}
config side_engine::new_config() const
{
config res = cfg_;
// Save default "recruit" so that correct faction lists would be
// initialized by flg_manager when the new side config is sent over network.
res["default_recruit"] = cfg_["recruit"].str();
// If the user is allowed to change type, faction, leader etc,
// then import their new values in the config.
if (!parent_.params_.saved_game) {
// Merge the faction data to res.
config faction = flg_.current_faction();
res["faction_name"] = faction["name"];
faction.remove_attributes("id", "name", "image", "gender", "type");
res.append(faction);
}
if (!cfg_.has_attribute("side") || cfg_["side"].to_int() != index_ + 1) {
res["side"] = index_ + 1;
}
res["controller"] = controller_names[controller_];
// the hosts recieves the serversided controller tweaks after the start event, but