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