The Battle for Wesnoth  1.15.1+dev
team.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Team-management, allies, setup at start of scenario.
18  */
19 
20 #include "team.hpp"
21 
22 #include "ai/manager.hpp"
23 #include "color.hpp"
24 #include "game_data.hpp"
25 #include "game_events/pump.hpp"
26 #include "lexical_cast.hpp"
27 #include "map/map.hpp"
28 #include "play_controller.hpp"
29 #include "preferences/game.hpp"
30 #include "resources.hpp"
32 #include "synced_context.hpp"
33 #include "units/types.hpp"
35 
36 #include <boost/dynamic_bitset.hpp>
37 
38 static lg::log_domain log_engine("engine");
39 #define DBG_NG LOG_STREAM(debug, log_engine)
40 #define LOG_NG LOG_STREAM(info, log_engine)
41 #define WRN_NG LOG_STREAM(warn, log_engine)
42 #define ERR_NG LOG_STREAM(err, log_engine)
43 
44 static lg::log_domain log_engine_enemies("engine/enemies");
45 #define DBG_NGE LOG_STREAM(debug, log_engine_enemies)
46 #define LOG_NGE LOG_STREAM(info, log_engine_enemies)
47 #define WRN_NGE LOG_STREAM(warn, log_engine_enemies)
48 
49 // Static member initialization
50 const int team::default_team_gold_ = 100;
51 
52 // Update this list of attributes if you change what is used to define a side
53 // (excluding those attributes used to define the side's leader).
54 const std::set<std::string> team::attributes {
55  "ai_config",
56  "carryover_add",
57  "carryover_percentage",
58  "color",
59  "controller",
60  "current_player",
61  "defeat_condition",
62  "flag",
63  "flag_icon",
64  "fog",
65  "fog_data",
66  "gold",
67  "hidden",
68  "income",
69  "no_leader",
70  "objectives",
71  "objectives_changed",
72  "persistent",
73  "lost",
74  "recall_cost",
75  "recruit",
76  "save_id",
77  "scroll_to_leader",
78  "share_vision",
79  "share_maps",
80  "share_view",
81  "shroud",
82  "shroud_data",
83  "start_gold",
84  "suppress_end_turn_confirmation",
85  "team_name",
86  "user_team_name",
87  "side_name",
88  "village_gold",
89  "village_support",
90  "is_local",
91  // Multiplayer attributes.
92  "player_id",
93  "is_host",
94  "action_bonus_count",
95  "allow_changes",
96  "allow_player",
97  "color_lock",
98  "countdown_time",
99  "disallow_observers",
100  "faction",
101  "faction_from_recruit",
102  "faction_name",
103  "faction_lock",
104  "gold_lock",
105  "income_lock",
106  "leader",
107  "leader_lock",
108  "random_leader",
109  "team_lock",
110  "terrain_liked",
111  "user_description",
112  "controller_lock",
113  "chose_random",
114  "disallow_shuffle",
115  "description"
116 };
117 
119  : gold(0)
120  , start_gold(0)
121  , income(0)
122  , income_per_village(0)
123  , support_per_village(1)
124  , minimum_recruit_price(0)
125  , recall_cost(0)
126  , can_recruit()
127  , team_name()
128  , user_team_name()
129  , side_name()
130  , faction()
131  , faction_name()
132  , save_id()
133  , current_player()
134  , countdown_time()
135  , action_bonus_count(0)
136  , flag()
137  , flag_icon()
138  , id()
139  , scroll_to_leader(true)
140  , objectives()
141  , objectives_changed(false)
142  , controller()
143  , is_local(true)
144  , defeat_condition(team::DEFEAT_CONDITION::NO_LEADER)
145  , proxy_controller(team::PROXY_CONTROLLER::PROXY_HUMAN)
146  , share_vision(team::SHARE_VISION::ALL)
147  , disallow_observers(false)
148  , allow_player(false)
149  , chose_random(false)
150  , no_leader(true)
151  , hidden(true)
152  , no_turn_confirmation(false)
153  , color()
154  , side(1)
155  , persistent(false)
156  , lost(false)
157  , carryover_percentage(game_config::gold_carryover_percentage)
158  , carryover_add(false)
159  , carryover_bonus(0)
160  , carryover_gold(0)
161 {
162 }
163 
165 {
166  gold = cfg["gold"];
167  income = cfg["income"];
168  team_name = cfg["team_name"].str();
169  user_team_name = cfg["user_team_name"];
170  side_name = cfg["side_name"];
171  faction = cfg["faction"].str();
172  faction_name = cfg["faction_name"];
173  save_id = cfg["save_id"].str();
174  current_player = cfg["current_player"].str();
175  countdown_time = cfg["countdown_time"].str();
176  action_bonus_count = cfg["action_bonus_count"];
177  flag = cfg["flag"].str();
178  flag_icon = cfg["flag_icon"].str();
179  id = cfg["id"].str();
180  scroll_to_leader = cfg["scroll_to_leader"].to_bool(true);
181  objectives = cfg["objectives"];
182  objectives_changed = cfg["objectives_changed"].to_bool();
183  disallow_observers = cfg["disallow_observers"].to_bool();
184  allow_player = cfg["allow_player"].to_bool(true);
185  chose_random = cfg["chose_random"].to_bool(false);
186  no_leader = cfg["no_leader"].to_bool();
187  defeat_condition = cfg["defeat_condition"].to_enum<team::DEFEAT_CONDITION>(team::DEFEAT_CONDITION::NO_LEADER);
188  lost = cfg["lost"].to_bool(false);
189  hidden = cfg["hidden"].to_bool();
190  no_turn_confirmation = cfg["suppress_end_turn_confirmation"].to_bool();
191  side = cfg["side"].to_int(1);
192  carryover_percentage = cfg["carryover_percentage"].to_int(game_config::gold_carryover_percentage);
193  carryover_add = cfg["carryover_add"].to_bool(false);
194  carryover_bonus = cfg["carryover_bonus"].to_double(1);
195  carryover_gold = cfg["carryover_gold"].to_int(0);
196  variables = cfg.child_or_empty("variables");
197  is_local = cfg["is_local"].to_bool(true);
198 
200 
201  // If starting new scenario override settings from [ai] tags
204 
206  if(cfg.has_attribute("ai_config")) {
207  ai::manager::get_singleton().add_ai_for_side_from_file(side, cfg["ai_config"], true);
208  } else {
210  }
211  }
212 
213  std::vector<std::string> recruits = utils::split(cfg["recruit"]);
214  can_recruit.insert(recruits.begin(), recruits.end());
215 
216  // at the start of a scenario "start_gold" is not set, we need to take the
217  // value from the gold setting (or fall back to the gold default)
218  if(!cfg["start_gold"].empty()) {
219  start_gold = cfg["start_gold"];
220  } else if(!cfg["gold"].empty()) {
221  start_gold = gold;
222  } else {
224  }
225 
226  if(team_name.empty()) {
227  team_name = cfg["side"].str();
228  }
229 
230  if(save_id.empty()) {
231  save_id = id;
232  }
233 
234  income_per_village = cfg["village_gold"].to_int(game_config::village_income);
235  recall_cost = cfg["recall_cost"].to_int(game_config::recall_cost);
236 
237  const std::string& village_support = cfg["village_support"];
238  if(village_support.empty()) {
240  } else {
242  }
243 
244  controller = team::CONTROLLER::AI;
245  controller.parse(cfg["controller"].str());
246 
247  // TODO: Why do we read disallow observers differently when controller is empty?
248  if(controller == CONTROLLER::EMPTY) {
249  disallow_observers = cfg["disallow_observers"].to_bool(true);
250  }
251 
252  // override persistence flag if it is explicitly defined in the config
253  // by default, persistence of a team is set depending on the controller
254  persistent = cfg["persistent"].to_bool(this->controller == CONTROLLER::HUMAN);
255 
256  //========================================================
257  // END OF MESSY CODE
258 
259  // Share_view and share_maps can't both be enabled,
260  // so share_view overrides share_maps.
261  share_vision = cfg["share_vision"].to_enum<team::SHARE_VISION>(team::SHARE_VISION::ALL);
263 
264  LOG_NG << "team_info::team_info(...): team_name: " << team_name << ", share_vision: " << share_vision << ".\n";
265 }
266 
268 {
269  if(cfg.has_attribute("share_view") || cfg.has_attribute("share_maps")) {
270  if(cfg["share_view"].to_bool()) {
271  share_vision = team::SHARE_VISION::ALL;
272  } else if(cfg["share_maps"].to_bool(true)) {
273  share_vision = team::SHARE_VISION::SHROUD;
274  } else {
276  }
277  }
278 }
279 
281 {
282  cfg["gold"] = gold;
283  cfg["start_gold"] = start_gold;
284  cfg["income"] = income;
285  cfg["team_name"] = team_name;
286  cfg["user_team_name"] = user_team_name;
287  cfg["side_name"] = side_name;
288  cfg["faction"] = faction;
289  cfg["faction_name"] = faction_name;
290  cfg["save_id"] = save_id;
291  cfg["current_player"] = current_player;
292  cfg["flag"] = flag;
293  cfg["flag_icon"] = flag_icon;
294  cfg["id"] = id;
295  cfg["objectives"] = objectives;
296  cfg["objectives_changed"] = objectives_changed;
297  cfg["countdown_time"] = countdown_time;
298  cfg["action_bonus_count"] = action_bonus_count;
299  cfg["village_gold"] = income_per_village;
300  cfg["village_support"] = support_per_village;
301  cfg["recall_cost"] = recall_cost;
302  cfg["disallow_observers"] = disallow_observers;
303  cfg["allow_player"] = allow_player;
304  cfg["chose_random"] = chose_random;
305  cfg["no_leader"] = no_leader;
306  cfg["defeat_condition"] = defeat_condition;
307  cfg["hidden"] = hidden;
308  cfg["suppress_end_turn_confirmation"] = no_turn_confirmation;
309  cfg["scroll_to_leader"] = scroll_to_leader;
310  cfg["controller"] = controller;
311  cfg["recruit"] = utils::join(can_recruit);
312  cfg["share_vision"] = share_vision;
313 
314  cfg["color"] = color;
315  cfg["persistent"] = persistent;
316  cfg["lost"] = lost;
317  cfg["carryover_percentage"] = carryover_percentage;
318  cfg["carryover_add"] = carryover_add;
319  cfg["carryover_bonus"] = carryover_bonus;
320  cfg["carryover_gold"] = carryover_gold;
321 
322  if(!variables.empty()) {
323  cfg.add_child("variables", variables);
324  }
325 
327 }
328 
330  : gold_(0)
331  , villages_()
332  , shroud_()
333  , fog_()
334  , fog_clearer_()
335  , auto_shroud_updates_(true)
336  , info_()
337  , countdown_time_(0)
339  , recall_list_()
340  , last_recruit_()
341  , enemies_()
342  , ally_shroud_()
343  , ally_fog_()
344  , planned_actions_()
345 {
346 }
347 
349 {
350 }
351 
352 void team::build(const config& cfg, const gamemap& map, int gold)
353 {
354  gold_ = gold;
355  info_.read(cfg);
356 
357  fog_.set_enabled(cfg["fog"].to_bool());
358  fog_.read(cfg["fog_data"]);
359  shroud_.set_enabled(cfg["shroud"].to_bool());
360  shroud_.read(cfg["shroud_data"]);
361  auto_shroud_updates_ = cfg["auto_shroud"].to_bool(auto_shroud_updates_);
362 
363  LOG_NG << "team::team(...): team_name: " << info_.team_name << ", shroud: " << uses_shroud()
364  << ", fog: " << uses_fog() << ".\n";
365 
366  // Load the WML-cleared fog.
367  const config& fog_override = cfg.child("fog_override");
368  if(fog_override) {
369  const std::vector<map_location> fog_vector
370  = map.parse_location_range(fog_override["x"], fog_override["y"], true);
371  fog_clearer_.insert(fog_vector.begin(), fog_vector.end());
372  }
373 
374  // To ensure some minimum starting gold,
375  // gold is the maximum of 'gold' and what is given in the config file
376  gold_ = std::max(gold, info_.gold);
377  if(gold_ != info_.gold) {
379  }
380 
381  // Old code was doing:
382  // info_.start_gold = std::to_string(gold) + " (" + info_.start_gold + ")";
383  // Was it correct?
384 
385  // Load in the villages the side controls at the start
386  for(const config& v : cfg.child_range("village")) {
387  map_location loc(v);
388  if(map.is_village(loc)) {
389  villages_.insert(loc);
390  } else {
391  WRN_NG << "[side] " << current_player() << " [village] points to a non-village location " << loc
392  << std::endl;
393  }
394  }
395 
396  countdown_time_ = cfg["countdown_time"];
397  action_bonus_count_ = cfg["action_bonus_count"];
398 
399  planned_actions_.reset(new wb::side_actions());
400  planned_actions_->set_team_index(info_.side - 1);
401 }
402 
403 void team::write(config& cfg) const
404 {
405  info_.write(cfg);
406  cfg["auto_shroud"] = auto_shroud_updates_;
407  cfg["shroud"] = uses_shroud();
408  cfg["fog"] = uses_fog();
409  cfg["gold"] = gold_;
410 
411  // Write village locations
412  for(const map_location& loc : villages_) {
413  loc.write(cfg.add_child("village"));
414  }
415 
416  cfg["shroud_data"] = shroud_.write();
417  cfg["fog_data"] = fog_.write();
418  if(!fog_clearer_.empty())
419  write_location_range(fog_clearer_, cfg.add_child("fog_override"));
420 
421  cfg["countdown_time"] = countdown_time_;
422  cfg["action_bonus_count"] = action_bonus_count_;
423 }
424 
425 void team::fix_villages(const gamemap &map)
426 {
427  for (auto it = villages_.begin(); it != villages_.end(); ) {
428  if (map.is_village(*it)) {
429  ++it;
430  }
431  else {
432  it = villages_.erase(it);
433  }
434  }
435 }
436 
438 {
439  villages_.insert(loc);
441 
442  if(gamedata) {
443  config::attribute_value& var = gamedata->get_variable("owner_side");
444  const config::attribute_value old_value = var;
445  var = owner_side;
446 
447  // During team building, game_events pump is not guaranteed to exist yet. (At current revision.) We skip capture
448  // events in this case.
450  res = resources::game_events->pump().fire("capture", loc);
451  }
452 
453  if(old_value.blank()) {
454  gamedata->clear_variable("owner_side");
455  } else {
456  var = old_value;
457  }
458  }
459 
460  return res;
461 }
462 
464 {
465  const std::set<map_location>::const_iterator vil = villages_.find(loc);
466  assert(vil != villages_.end());
467  villages_.erase(vil);
468 }
469 
470 void team::set_recruits(const std::set<std::string>& recruits)
471 {
475 }
476 
477 void team::add_recruit(const std::string& recruit)
478 {
479  info_.can_recruit.insert(recruit);
482 }
483 
485 {
488  }
489  int min = 20;
490  for(std::string recruit : info_.can_recruit) {
491  const unit_type* ut = unit_types.find(recruit);
492  if(!ut) {
493  continue;
494  } else {
495  if(ut->cost() < min) {
496  min = ut->cost();
497  }
498  }
499  }
500 
502 
504 }
505 
506 void team::calculate_enemies(std::size_t index) const
507 {
508  if(!resources::gameboard || index >= resources::gameboard->teams().size()) {
509  return;
510  }
511 
512  while(enemies_.size() <= index) {
513  enemies_.push_back(calculate_is_enemy(enemies_.size()));
514  }
515 }
516 
517 bool team::calculate_is_enemy(std::size_t index) const
518 {
519  // We're not enemies of ourselves
520  if(&resources::gameboard->teams()[index] == this) {
521  return false;
522  }
523 
524  // We are friends with anyone who we share a teamname with
525  std::vector<std::string> our_teams = utils::split(info_.team_name);
526  std::vector<std::string> their_teams = utils::split(resources::gameboard->teams()[index].info_.team_name);
527 
528  LOG_NGE << "team " << info_.side << " calculates if it has enemy in team " << index + 1 << "; our team_name ["
529  << info_.team_name << "], their team_name is [" << resources::gameboard->teams()[index].info_.team_name
530  << "]" << std::endl;
531 
532  for(const std::string& t : our_teams) {
533  if(std::find(their_teams.begin(), their_teams.end(), t) != their_teams.end()) {
534  LOG_NGE << "team " << info_.side << " found same team name [" << t << "] in team " << index + 1
535  << std::endl;
536  return false;
537  } else {
538  LOG_NGE << "team " << info_.side << " not found same team name [" << t << "] in team " << index + 1
539  << std::endl;
540  }
541  }
542 
543  LOG_NGE << "team " << info_.side << " has enemy in team " << index + 1 << std::endl;
544  return true;
545 }
546 
547 namespace
548 {
549 class controller_server_choice : public synced_context::server_choice
550 {
551 public:
552  controller_server_choice(team::CONTROLLER new_controller, const team& team)
553  : new_controller_(new_controller)
554  , team_(team)
555  {
556  }
557 
558  /// We are in a game with no mp server and need to do this choice locally
559  virtual config local_choice() const
560  {
561  return config{"controller", new_controller_, "is_local", true};
562  }
563 
564  /// tThe request which is sent to the mp server.
565  virtual config request() const
566  {
567  return config{
568  "new_controller", new_controller_, "old_controller", team_.controller(), "side", team_.side(),
569  };
570  }
571 
572  virtual const char* name() const
573  {
574  return "change_controller_wml";
575  }
576 
577 private:
578  team::CONTROLLER new_controller_;
579  const team& team_;
580 };
581 } // end anon namespace
582 
583 void team::change_controller_by_wml(const std::string& new_controller_string)
584 {
585  CONTROLLER new_controller;
586  if(!new_controller.parse(new_controller_string)) {
587  WRN_NG << "ignored attempt to change controller to " << new_controller_string << std::endl;
588  return;
589  }
590 
591  if(new_controller == CONTROLLER::EMPTY && resources::controller->current_side() == this->side()) {
592  WRN_NG << "ignored attempt to change the currently playing side's controller to 'null'" << std::endl;
593  return;
594  }
595 
596  config choice = synced_context::ask_server_choice(controller_server_choice(new_controller, *this));
597  if(!new_controller.parse(choice["controller"])) {
598  // TODO: this should be more than a ERR_NG message.
599  // GL-2016SEP02 Oh? So why was ERR_NG defined as warning level? Making the call fit the definition.
600  WRN_NG << "Received an invalid controller string from the server" << choice["controller"] << std::endl;
601  }
602 
603  if(!resources::controller->is_replay()) {
604  set_local(choice["is_local"].to_bool());
605  }
606 
607  change_controller(new_controller);
608 }
609 
610 void team::change_team(const std::string& name, const t_string& user_name)
611 {
612  info_.team_name = name;
613 
614  if(!user_name.empty()) {
615  info_.user_team_name = user_name;
616  } else {
618  }
619 
620  clear_caches();
621 }
622 
624 {
625  // Reset the cache of allies for all teams
627  for(auto& t : resources::gameboard->teams()) {
628  t.enemies_.clear();
629  t.ally_shroud_.clear();
630  t.ally_fog_.clear();
631  }
632  }
633 }
634 
635 void team::set_objectives(const t_string& new_objectives, bool silently)
636 {
637  info_.objectives = new_objectives;
638 
639  if(!silently) {
640  info_.objectives_changed = true;
641  }
642 }
643 
644 bool team::shrouded(const map_location& loc) const
645 {
646  if(!resources::gameboard) {
647  return shroud_.value(loc.wml_x(), loc.wml_y());
648  }
649 
650  return shroud_.shared_value(ally_shroud(resources::gameboard->teams()), loc.wml_x(), loc.wml_y());
651 }
652 
653 bool team::fogged(const map_location& loc) const
654 {
655  if(shrouded(loc)) {
656  return true;
657  }
658 
659  // Check for an override of fog.
660  if(fog_clearer_.count(loc) > 0) {
661  return false;
662  }
663 
664  if(!resources::gameboard) {
665  return fog_.value(loc.wml_x(), loc.wml_y());
666  }
667 
668  return fog_.shared_value(ally_fog(resources::gameboard->teams()), loc.wml_x(), loc.wml_y());
669 }
670 
671 const std::vector<const team::shroud_map*>& team::ally_shroud(const std::vector<team>& teams) const
672 {
673  if(ally_shroud_.empty()) {
674  for(std::size_t i = 0; i < teams.size(); ++i) {
675  if(!is_enemy(i + 1) && (&(teams[i]) == this || teams[i].share_view() || teams[i].share_maps())) {
676  ally_shroud_.push_back(&(teams[i].shroud_));
677  }
678  }
679  }
680 
681  return ally_shroud_;
682 }
683 
684 const std::vector<const team::shroud_map*>& team::ally_fog(const std::vector<team>& teams) const
685 {
686  if(ally_fog_.empty()) {
687  for(std::size_t i = 0; i < teams.size(); ++i) {
688  if(!is_enemy(i + 1) && (&(teams[i]) == this || teams[i].share_view())) {
689  ally_fog_.push_back(&(teams[i].fog_));
690  }
691  }
692  }
693 
694  return ally_fog_;
695 }
696 
697 bool team::knows_about_team(std::size_t index) const
698 {
699  const team& t = resources::gameboard->teams()[index];
700 
701  // We know about our own team
702  if(this == &t) {
703  return true;
704  }
705 
706  // If we aren't using shroud or fog, then we know about everyone
707  if(!uses_shroud() && !uses_fog()) {
708  return true;
709  }
710 
711  // We don't know about enemies
712  if(is_enemy(index + 1)) {
713  return false;
714  }
715 
716  // We know our human allies.
717  if(t.is_human()) {
718  return true;
719  }
720 
721  // We know about allies we're sharing maps with
722  if(share_maps() && t.uses_shroud()) {
723  return true;
724  }
725 
726  // We know about allies we're sharing view with
727  if(share_view() && (t.uses_fog() || t.uses_shroud())) {
728  return true;
729  }
730 
731  return false;
732 }
733 
734 /**
735  * Removes the record of hexes that were cleared of fog via WML.
736  * @param[in] hexes The hexes to no longer keep clear.
737  */
738 void team::remove_fog_override(const std::set<map_location>& hexes)
739 {
740  // Take a set difference.
741  std::vector<map_location> result(fog_clearer_.size());
743  std::set_difference(fog_clearer_.begin(), fog_clearer_.end(), hexes.begin(), hexes.end(), result.begin());
744 
745  // Put the result into fog_clearer_.
746  fog_clearer_.clear();
747  fog_clearer_.insert(result.begin(), result_end);
748 }
749 
751 {
752  if(!resources::gameboard) {
753  return;
754  }
755 
756  if(side < 1 || side > static_cast<int>(resources::gameboard->teams().size())) {
757  throw game::game_error("invalid side(" + std::to_string(side) + ") found in unit definition");
758  }
759 }
760 
761 bool team::shroud_map::clear(int x, int y)
762 {
763  if(enabled_ == false || x < 0 || y < 0) {
764  return false;
765  }
766 
767  if(x >= static_cast<int>(data_.size())) {
768  data_.resize(x + 1);
769  }
770 
771  if(y >= static_cast<int>(data_[x].size())) {
772  data_[x].resize(y + 1);
773  }
774 
775  if(data_[x][y] == false) {
776  data_[x][y] = true;
777  return true;
778  }
779 
780  return false;
781 }
782 
783 void team::shroud_map::place(int x, int y)
784 {
785  if(enabled_ == false || x < 0 || y < 0) {
786  return;
787  }
788 
789  if(x >= static_cast<int>(data_.size())) {
790  DBG_NG << "Couldn't place shroud on invalid x coordinate: (" << x << ", " << y
791  << ") - max x: " << data_.size() - 1 << "\n";
792  } else if(y >= static_cast<int>(data_[x].size())) {
793  DBG_NG << "Couldn't place shroud on invalid y coordinate: (" << x << ", " << y
794  << ") - max y: " << data_[x].size() - 1 << "\n";
795  } else {
796  data_[x][y] = false;
797  }
798 }
799 
801 {
802  if(enabled_ == false) {
803  return;
804  }
805 
806  for(auto& i : data_) {
807  std::fill(i.begin(), i.end(), false);
808  }
809 }
810 
811 bool team::shroud_map::value(int x, int y) const
812 {
813  if(!enabled_) {
814  return false;
815  }
816 
817  // Locations for which we have no data are assumed to still be covered.
818  if(x < 0 || x >= static_cast<int>(data_.size())) {
819  return true;
820  }
821 
822  if(y < 0 || y >= static_cast<int>(data_[x].size())) {
823  return true;
824  }
825 
826  // data_ stores whether or not a location has been cleared, while
827  // we want to return whether or not a location is covered.
828  return !data_[x][y];
829 }
830 
831 bool team::shroud_map::shared_value(const std::vector<const shroud_map*>& maps, int x, int y) const
832 {
833  if(!enabled_) {
834  return false;
835  }
836 
837  // A quick abort:
838  if(x < 0 || y < 0) {
839  return true;
840  }
841 
842  // A tile is uncovered if it is uncovered on any shared map.
843  for(const shroud_map* const shared_map : maps) {
844  if(shared_map->enabled_ && !shared_map->value(x, y)) {
845  return false;
846  }
847  }
848 
849  return true;
850 }
851 
852 std::string team::shroud_map::write() const
853 {
854  std::stringstream shroud_str;
855  for(const auto& sh : data_) {
856  shroud_str << '|';
857 
858  for(bool i : sh) {
859  shroud_str << (i ? '1' : '0');
860  }
861 
862  shroud_str << '\n';
863  }
864 
865  return shroud_str.str();
866 }
867 
868 void team::shroud_map::read(const std::string& str)
869 {
870  data_.clear();
871 
872  for(const char sh : str) {
873  if(sh == '|') {
874  data_.resize(data_.size() + 1);
875  }
876 
877  if(data_.empty() == false) {
878  if(sh == '1') {
879  data_.back().push_back(true);
880  } else if(sh == '0') {
881  data_.back().push_back(false);
882  }
883  }
884  }
885 }
886 
887 void team::shroud_map::merge(const std::string& str)
888 {
889  int x = 0, y = 0;
890  for(std::size_t i = 1; i < str.length(); ++i) {
891  if(str[i] == '|') {
892  y = 0;
893  x++;
894  } else if(str[i] == '1') {
895  clear(x, y);
896  y++;
897  } else if(str[i] == '0') {
898  y++;
899  }
900  }
901 }
902 
903 bool team::shroud_map::copy_from(const std::vector<const shroud_map*>& maps)
904 {
905  if(enabled_ == false) {
906  return false;
907  }
908 
909  bool cleared = false;
910  for(const shroud_map* m : maps) {
911  if(m->enabled_ == false) {
912  continue;
913  }
914 
915  const std::vector<std::vector<bool>>& v = m->data_;
916  for(std::size_t x = 0; x != v.size(); ++x) {
917  for(std::size_t y = 0; y != v[x].size(); ++y) {
918  if(v[x][y]) {
919  cleared |= clear(x, y);
920  }
921  }
922  }
923  }
924 
925  return cleared;
926 }
927 
929 {
930  std::string index = get_side_color_id(side);
932 
933  if(gp != game_config::team_rgb_range.end()) {
934  return (gp->second);
935  }
936 
937  return color_range({255, 0, 0}, {255, 255, 255}, {0, 0, 0}, {255, 0, 0});
938 }
939 
941 {
942  return get_side_color_range(side).mid();
943 }
944 
946 {
947  // Note: use mid() instead of rep() unless
948  // high contrast is needed over a map or minimap!
949  return get_side_color_range(side).rep();
950 }
951 
952 std::string team::get_side_color_id(unsigned side)
953 {
954  try {
955  const unsigned index = side - 1;
956 
957  // If no gameboard (and by extension, team list) is available, use the default side color.
958  if(!resources::gameboard) {
959  return game_config::default_colors.at(index);
960  }
961 
962  // Else, try to fetch the color from the side's config.
963  const std::string& side_color = resources::gameboard->teams().at(index).color();
964 
965  if(!side_color.empty()) {
966  return side_color;
967  }
968 
969  // If the side color data was empty, fall back to the default color. This should only
970  // happen if the side data hadn't been initialized yet, which is the case if this function
971  // is being called to set up said side data. :P
972  return game_config::default_colors.at(index);
973  } catch(const std::out_of_range&) {
974  // Side index was invalid! Coloring will fail!
975  return "";
976  }
977 }
978 
980 {
981  const config::attribute_value& c = cfg["color"];
982 
983  // If no color key or value was provided, use the given color for that side.
984  // If outside a game context (ie, where a list of teams has been constructed),
985  // this will just be the side's default color.
986  if(c.blank() || c.empty()) {
987  return get_side_color_id(cfg["side"].to_unsigned());
988  }
989 
990  // Do the same as above for numeric color key values.
991  if(unsigned side = c.to_unsigned()) {
992  return get_side_color_id(side);
993  }
994 
995  // Else, we should have a color id at this point. Return it.
996  return c.str();
997 }
998 
1000 {
1001  return get_side_color_range(side).mid().to_hex_string();
1002 }
1003 
1005 {
1006  LOG_NG << "Adding recruitable units: \n";
1007  for(const std::string& recruit : info_.can_recruit) {
1008  LOG_NG << recruit << std::endl;
1009  }
1010 
1011  LOG_NG << "Added all recruitable units\n";
1012 }
1013 
1015 {
1016  config cfg;
1017  config& result = cfg.add_child("side");
1018  write(result);
1019  return result;
1020 }
1021 
1022 std::string team::allied_human_teams() const
1023 {
1024  std::vector<int> res;
1025  for(const team& t : resources::gameboard->teams()) {
1026  if(!t.is_enemy(this->side()) && t.is_human()) {
1027  res.push_back(t.side());
1028  }
1029  }
1030 
1031  return utils::join(res);
1032 }
play_controller * controller
Definition: resources.cpp:21
bool empty() const
Tests for an attribute that either was never set or was set to "".
std::string id
Definition: team.hpp:125
bool allow_player
Definition: team.hpp:145
static const int default_team_gold_
Definition: team.hpp:166
void place(int x, int y)
Definition: team.cpp:783
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:420
bool no_turn_confirmation
Definition: team.hpp:149
std::string last_recruit_
Definition: team.hpp:432
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1267
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:92
DEFEAT_CONDITION defeat_condition
Definition: team.hpp:138
std::map< std::string, color_range > team_rgb_range
unsigned to_unsigned(unsigned def=0) const
SHARE_VISION share_vision
Definition: team.hpp:143
int village_support
Definition: game_config.cpp:55
void handle_legacy_share_vision(const config &cfg)
Definition: team.cpp:267
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
CONTROLLER controller
Definition: team.hpp:136
void set_objectives(const t_string &new_objectives, bool silently=false)
Definition: team.cpp:635
int carryover_gold
Definition: team.hpp:161
static std::string get_side_color_id_from_config(const config &cfg)
Definition: team.cpp:979
static manager & get_singleton()
Definition: manager.hpp:150
#define DBG_NG
Definition: team.cpp:39
int minimum_recruit_price() const
Definition: team.cpp:484
void write(config &cfg) const
Definition: team.cpp:280
Variant for storing WML attributes.
New lexcical_cast header.
bool has_attribute(config_key_type key) const
Definition: config.cpp:213
static const color_range get_side_color_range(int side)
Definition: team.cpp:928
static bool has_manager()
Definition: manager.hpp:156
int support_per_village
Definition: team.hpp:107
bool copy_from(const std::vector< const shroud_map *> &maps)
Definition: team.cpp:903
t_string objectives
Definition: team.hpp:129
void calculate_enemies(std::size_t index) const
Definition: team.cpp:506
bool share_view() const
Definition: team.hpp:387
void fix_villages(const gamemap &map)
Definition: team.cpp:425
static const std::set< std::string > attributes
Stores the attributes recognized by [side].
Definition: team.hpp:174
child_itors child_range(config_key_type key)
Definition: config.cpp:362
void clear(const std::string &key)
Definition: general.cpp:205
bool value(int x, int y) const
Definition: team.cpp:811
int wml_x() const
Definition: location.hpp:157
#define WRN_NG
Definition: team.cpp:41
unit_type_data unit_types
Definition: types.cpp:1525
void build(const config &cfg, const gamemap &map, int gold=default_team_gold_)
Definition: team.cpp:352
int minimum_recruit_price
Definition: team.hpp:108
team()
Definition: team.cpp:329
t_string faction_name
Definition: team.hpp:115
std::shared_ptr< wb::side_actions > planned_actions_
Whiteboard planned actions for this team.
Definition: team.hpp:444
std::string flag_icon
Definition: team.hpp:123
bool objectives_changed
< Team&#39;s objectives for the current level.
Definition: team.hpp:134
void add_recruit(const std::string &)
Definition: team.cpp:477
-file sdl_utils.hpp
void change_controller(const std::string &new_controller)
Definition: team.hpp:274
std::string faction
Definition: team.hpp:114
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
int village_support() const
Definition: team.hpp:199
A single unit type that the player may recruit.
Definition: types.hpp:42
double carryover_bonus
Definition: team.hpp:160
game_data * gamedata
Definition: resources.cpp:22
config::attribute_value & get_variable(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:62
bool calculate_is_enemy(std::size_t index) const
Definition: team.cpp:517
static std::string get_side_highlight_pango(int side)
Definition: team.cpp:999
std::string flag_icon
bool uses_fog() const
Definition: team.hpp:317
int gold() const
Definition: team.hpp:189
shroud_map shroud_
Definition: team.hpp:420
static lg::log_domain log_engine_enemies("engine/enemies")
bool knows_about_team(std::size_t index) const
Definition: team.cpp:697
team_info info_
Definition: team.hpp:426
bool add_ai_for_side_from_file(side_number side, const std::string &file, bool replace=true)
Adds active AI for specified side from file.
Definition: manager.cpp:607
std::string team_name
Definition: team.hpp:111
void write(config &cfg) const
Definition: team.cpp:403
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
void raise_recruit_list_changed()
Notifies all observers of &#39;ai_recruit_list_changed&#39; event.
Definition: manager.cpp:440
std::vector< map_location > parse_location_range(const std::string &xvals, const std::string &yvals, bool with_border=false) const
Parses ranges of locations into a vector of locations, using this map&#39;s dimensions as bounds...
Definition: map.cpp:416
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
std::string allied_human_teams() const
Definition: team.cpp:1022
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
bool chose_random
Definition: team.hpp:146
int cost() const
Definition: types.hpp:157
bool is_local
Definition: team.hpp:137
void read(const config &cfg)
Definition: team.cpp:164
bool uses_shroud() const
Definition: team.hpp:316
config variables
Definition: team.hpp:162
int wml_y() const
Definition: location.hpp:158
const int gold_carryover_percentage
Default percentage gold carried over to the next scenario.
Definition: game_config.cpp:63
void set_local(bool local)
Definition: team.hpp:271
bool blank() const
Tests for an attribute that was never set.
game_board * gameboard
Definition: resources.cpp:20
bool shared_value(const std::vector< const shroud_map *> &maps, int x, int y) const
Definition: team.cpp:831
Encapsulates the map of the game.
Definition: map.hpp:36
bool disallow_observers
Definition: team.hpp:144
bool is_enemy(int n) const
Definition: team.hpp:243
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands...
void merge(const std::string &shroud_data)
Definition: team.cpp:887
const std::vector< const shroud_map * > & ally_fog(const std::vector< team > &teams) const
Definition: team.cpp:684
bool persistent
Definition: team.hpp:154
Error used for any general game error, e.g.
Definition: game_errors.hpp:46
bool auto_shroud_updates_
Definition: team.hpp:424
void remove_fog_override(const std::set< map_location > &hexes)
Removes the record of hexes that were cleared of fog via WML.
Definition: team.cpp:738
std::vector< const shroud_map * > ally_fog_
Definition: team.hpp:439
game_events::manager * game_events
Definition: resources.cpp:24
boost::dynamic_bitset enemies_
Definition: team.hpp:437
static std::string get_side_color_id(unsigned side)
Definition: team.cpp:952
Encapsulates the map of the game.
Definition: location.hpp:42
bool shrouded(const map_location &loc) const
Definition: team.cpp:644
static void clear_caches()
clear the shroud, fog, and enemies cache for all teams
Definition: team.cpp:623
std::string current_player
Definition: team.hpp:118
int start_gold
Definition: team.hpp:104
std::size_t i
Definition: function.cpp:933
virtual ~team()
Definition: team.cpp:348
void write_location_range(const std::set< map_location > &locs, config &cfg)
Write a set of locations into a config using ranges, adding keys x=x1,..,xn and y=y1a-y1b,..,yna-ynb.
Definition: location.cpp:441
int carryover_percentage
Definition: team.hpp:157
Default, unset return value.
Definition: retval.hpp:31
void read(const std::string &shroud_data)
Definition: team.cpp:868
int countdown_time_
Definition: team.hpp:428
void set_enabled(bool enabled)
Definition: team.hpp:92
Game configuration data as global variables.
Definition: build_info.cpp:49
std::set< map_location > villages_
Definition: team.hpp:418
int village_income
Definition: game_config.cpp:54
#define LOG_NGE
Definition: team.cpp:46
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:97
Define the game&#39;s event mechanism.
A color range definition is made of four reference RGB colors, used for calculating conversions from ...
Definition: color_range.hpp:50
const std::string & current_player() const
Definition: team.hpp:234
int recall_cost
Definition: team.hpp:109
static t_string from_serialized(const std::string &string)
Definition: tstring.hpp:149
void validate_side(int side)
Definition: team.cpp:750
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
bool add_ai_for_side_from_config(side_number side, const config &cfg, bool replace=true)
Adds active AI for specified side from cfg.
Definition: manager.cpp:618
std::vector< const shroud_map * > ally_shroud_
Definition: team.hpp:439
recall_list_manager recall_list_
Definition: team.hpp:431
void change_controller_by_wml(const std::string &new_controller)
Definition: team.cpp:583
bool clear(int x, int y)
Definition: team.cpp:761
static lg::log_domain log_engine("engine")
config & add_child(config_key_type key)
Definition: config.cpp:476
#define LOG_NG
Definition: team.cpp:40
pump_result_t fire(const std::string &event, const entity_location &loc1=entity_location::null_entity, const entity_location &loc2=entity_location::null_entity, const config &data=config())
Function to fire an event.
Definition: pump.cpp:486
bool is_village(const map_location &loc) const
Definition: map.cpp:65
game_events::pump_result_t get_village(const map_location &, const int owner_side, game_data *fire_event)
Acquires a village from owner_side. Pointer fire_event should be the game_data for the game if it is ...
Definition: team.cpp:437
bool is_human() const
Definition: team.hpp:263
static color_t get_minimap_color(int side)
Definition: team.cpp:945
bool empty() const
Definition: tstring.hpp:182
void reset()
Definition: team.cpp:800
bool share_maps() const
Definition: team.hpp:386
std::set< map_location > fog_clearer_
Stores hexes that have been cleared of fog via WML.
Definition: team.hpp:422
double t
Definition: astarsearch.cpp:64
t_string side_name
Definition: team.hpp:113
color_t rep() const
High-contrast shade, intended for the minimap markers.
Definition: color_range.hpp:96
bool find(E event, F functor)
Tests whether an event handler is available.
void lose_village(const map_location &)
Definition: team.cpp:463
shroud_map fog_
Definition: team.hpp:420
std::string color
Definition: team.hpp:151
int income_per_village
Definition: team.hpp:106
int gold_
Definition: team.hpp:417
t_string user_team_name
Definition: team.hpp:112
static color_t get_side_color(int side)
Definition: team.cpp:940
bool carryover_add
Definition: team.hpp:158
int action_bonus_count_
Definition: team.hpp:429
int action_bonus_count
Definition: team.hpp:120
void log_recruitable() const
Definition: team.cpp:1004
void change_team(const std::string &name, const t_string &user_name)
Definition: team.cpp:610
std::set< std::string > can_recruit
Definition: team.hpp:110
std::string write() const
Definition: team.cpp:852
bool fogged(const map_location &loc) const
Definition: team.cpp:653
std::string save_id
Definition: team.hpp:116
bool translatable() const
Definition: tstring.hpp:188
game_events::wml_event_pump & pump()
Definition: manager.cpp:229
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:114
void set_recruits(const std::set< std::string > &recruits)
Definition: team.cpp:470
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:453
const std::vector< const shroud_map * > & ally_shroud(const std::vector< team > &teams) const
Definition: team.cpp:671
std::vector< std::string > default_colors
std::tuple< bool, bool > pump_result_t
Definition: fwd.hpp:28
int side() const
Definition: team.hpp:188
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
mock_char c
std::string flag
Definition: team.hpp:122
This internal whiteboard class holds the planned action queues for a team, and offers many utility me...
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
bool empty() const
Definition: config.cpp:884
std::string countdown_time
Definition: team.hpp:119
bool scroll_to_leader
Definition: team.hpp:127
bool no_leader
Definition: team.hpp:147
color_t mid() const
Average color shade.
Definition: color_range.hpp:87
const std::set< std::string > & recruits() const
Definition: team.hpp:223
static config ask_server_choice(const server_choice &)
std::string str(const std::string &fallback="") const
config to_config() const
Definition: team.cpp:1014