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