56 #include <boost/dynamic_bitset.hpp> 59 #pragma warning (push) 60 #pragma warning (disable: 4510 4610) 62 #include <boost/range/algorithm.hpp> 78 #define DBG_UT LOG_STREAM(debug, log_unit) 79 #define LOG_UT LOG_STREAM(info, log_unit) 80 #define WRN_UT LOG_STREAM(warn, log_unit) 81 #define ERR_UT LOG_STREAM(err, log_unit) 86 const std::array<std::string, 4> ModificationTypes {{
"advancement",
"advance",
"trait",
"object" }};
93 static std::vector<const unit*> units_with_cache;
95 static const std::string leader_crown_path =
"misc/leader-crown.png";
96 static std::array<std::string, 60> internalized_attrs {{
160 struct internalized_attrs_sorter
162 internalized_attrs_sorter()
164 std::sort(std::begin(internalized_attrs), std::end(internalized_attrs));
169 internalized_attrs_sorter sorter;
176 auto cur_known = internalized_attrs.begin();
177 auto end_known = internalized_attrs.end();
179 while(cur_known != end_known) {
183 int comp = cur->first.compare(*cur_known);
185 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded." << std::endl;
198 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded." << std::endl;
210 if(type_id.empty()) {
213 std::string new_id = type_id;
222 const std::vector<unit_race::GENDER>& genders = type.
genders();
223 assert(genders.size() > 0);
225 if(random_gender ==
false || genders.size() == 1) {
226 return genders.front();
239 const std::string& gender = cfg[
"gender"];
240 if(!gender.empty()) {
249 :
std::enable_shared_from_this<
unit>()
251 , advances_to_(o.advances_to_)
253 , type_name_(o.type_name_)
257 , underlying_id_(o.underlying_id_)
258 , undead_variation_(o.undead_variation_)
259 , variation_(o.variation_)
260 , hit_points_(o.hit_points_)
261 , max_hit_points_(o.max_hit_points_)
262 , experience_(o.experience_)
263 , max_experience_(o.max_experience_)
265 , recall_cost_(o.recall_cost_)
266 , canrecruit_(o.canrecruit_)
267 , recruit_list_(o.recruit_list_)
268 , alignment_(o.alignment_)
269 , flag_rgb_(o.flag_rgb_)
270 , image_mods_(o.image_mods_)
271 , unrenamable_(o.unrenamable_)
275 , movement_(o.movement_)
276 , max_movement_(o.max_movement_)
278 , jamming_(o.jamming_)
279 , movement_type_(o.movement_type_)
280 , hold_position_(o.hold_position_)
281 , end_turn_(o.end_turn_)
282 , resting_(o.resting_)
283 , attacks_left_(o.attacks_left_)
284 , max_attacks_(o.max_attacks_)
286 , known_boolean_states_(o.known_boolean_states_)
287 , variables_(o.variables_)
289 , filter_recall_(o.filter_recall_)
290 , emit_zoc_(o.emit_zoc_)
291 , overlays_(o.overlays_)
293 , attacks_(o.attacks_)
295 , trait_names_(o.trait_names_)
296 , trait_descriptions_(o.trait_descriptions_)
297 , unit_value_(o.unit_value_)
299 , interrupted_move_(o.interrupted_move_)
300 , is_fearless_(o.is_fearless_)
301 , is_healthy_(o.is_healthy_)
302 , modification_descriptions_(o.modification_descriptions_)
305 , hp_bar_scaling_(o.hp_bar_scaling_)
306 , xp_bar_scaling_(o.xp_bar_scaling_)
307 , modifications_(o.modifications_)
308 , abilities_(o.abilities_)
309 , advancements_(o.advancements_)
310 , description_(o.description_)
311 , special_notes_(o.special_notes_)
314 , ellipse_(o.ellipse_)
315 , random_traits_(o.random_traits_)
316 , generate_name_(o.generate_name_)
318 , profile_(o.profile_)
319 , small_profile_(o.small_profile_)
320 , changed_attributes_(o.changed_attributes_)
321 , invisibility_cache_()
330 :
std::enable_shared_from_this<
unit>()
408 type_ = &
get_unit_type(cfg[
"parent_type"].blank() ? cfg[
"type"].str() : cfg[
"parent_type"].str());
410 id_ = cfg[
"id"].str();
411 name_ = cfg[
"name"].t_str();
415 role_ = cfg[
"role"].str();
418 hidden_ = cfg[
"hidden"].to_bool(
false);
423 side_ = cfg[
"side"].to_int();
434 const vconfig& filter_recall = vcfg->
child(
"filter_recall");
435 if(!filter_recall.
null())
458 if(
const config& mods = cfg.
child(
"modifications")) {
470 deprecated_message(
"[unit]overlays",
DEP_LEVEL::PREEMPTIVE, {1, 17, 0},
"This warning is only triggered by the cases that *do* still work: setting [unit]overlays= works, but the [unit]overlays attribute will always be empty if WML tries to read it.");
473 effect[
"apply_to"] =
"overlay";
474 effect[
"add"] = v->str();
550 if(temp_advances.size() == 1 && temp_advances.front() ==
"null") {
552 }
else if(temp_advances.size() >= 1 && !temp_advances.front().empty()) {
564 for(
const config&
c : cfg_range) {
573 for(
const config&
c : cfg_range) {
592 for(
const config& abilities : cfg_range) {
616 if(
const config& status_flags = cfg.
child(
"status")) {
618 if(st.second.to_bool()) {
624 if(cfg[
"ai_special"] ==
"guardian") {
645 resting_ = cfg[
"resting"].to_bool();
650 if(!cfg[
"recall_cost"].blank()) {
664 for(
const auto& anim :
anim_comp_->animations_) {
665 std::cout << anim.debug() << std::endl;
672 for(
auto& u : units_with_cache) {
673 u->clear_visibility_cache();
676 units_with_cache.clear();
710 auto itor = std::find(units_with_cache.begin(), units_with_cache.end(),
this);
712 if(itor != units_with_cache.end()) {
713 units_with_cache.erase(itor);
715 }
catch(
const std::exception &
e) {
716 ERR_UT <<
"Caught exception when destroying unit: " << e.what() << std::endl;
798 LOG_UT <<
"Generating a trait for unit type " <<
type().
log_id() <<
" with must_have_only " << must_have_only << std::endl;
803 std::vector<const config*> candidate_traits;
807 const std::string& tid =
t[
"id"];
808 bool already =
false;
809 for(
const config& mod : current_traits) {
810 if(mod[
"id"] == tid) {
821 const std::string& avl =
t[
"availability"];
822 if(avl ==
"musthave") {
830 if(!must_have_only && (!
can_recruit() || avl ==
"any")) {
831 candidate_traits.push_back(&
t);
835 if(must_have_only)
return;
839 int nb_traits = current_traits.size();
841 for(; nb_traits < max_traits && !candidate_traits.empty(); ++nb_traits)
845 candidate_traits.erase(candidate_traits.begin() + num);
855 std::vector<std::string> res;
861 res.push_back(mod[
"id"]);
896 if(!new_type.
usage().empty()) {
901 if(!new_type.
ellipse().empty()) {
946 return std::make_shared<attack_type>(atk);
1027 return leader_crown_path;
1037 double unit_energy = 0.0;
1038 color_t energy_color {0,0,0,255};
1040 if(max_hitpoints > 0) {
1041 unit_energy =
static_cast<double>(
hitpoints)/static_cast<double>(max_hitpoints);
1044 if(1.0 == unit_energy) {
1045 energy_color.
r = 33;
1046 energy_color.g = 225;
1048 }
else if(unit_energy > 1.0) {
1049 energy_color.r = 100;
1050 energy_color.g = 255;
1051 energy_color.b = 100;
1052 }
else if(unit_energy >= 0.75) {
1053 energy_color.r = 170;
1054 energy_color.g = 255;
1056 }
else if(unit_energy >= 0.5) {
1057 energy_color.r = 255;
1058 energy_color.g = 175;
1060 }
else if(unit_energy >= 0.25) {
1061 energy_color.r = 255;
1062 energy_color.g = 155;
1065 energy_color.r = 255;
1070 return energy_color;
1090 const color_t near_advance_color {255,255,255,255};
1091 const color_t mid_advance_color {150,255,255,255};
1092 const color_t far_advance_color {0,205,205,255};
1093 const color_t normal_color {0,160,225,255};
1094 const color_t near_amla_color {225,0,255,255};
1095 const color_t mid_amla_color {169,30,255,255};
1096 const color_t far_amla_color {139,0,237,255};
1097 const color_t amla_color {170,0,255,255};
1106 color=near_advance_color;
1107 }
else if(mid_advance){
1108 color=mid_advance_color;
1109 }
else if(far_advance){
1110 color=far_advance_color;
1112 }
else if(has_amla){
1114 color=near_amla_color;
1115 }
else if(mid_advance){
1116 color=mid_amla_color;
1117 }
else if(far_advance){
1118 color=far_amla_color;
1129 bool major_amla =
false;
1130 bool has_amla =
false;
1132 major_amla |= adv[
"major_amla"].to_bool();
1147 std::vector<std::string> result;
1150 result.push_back(adv_type->type_name());
1152 WRN_UT <<
"unknown unit in advances_to list of type " 1153 <<
type().
log_id() <<
": " << adv_type_id << std::endl;
1185 if(goal_dur.empty()) {
1187 return !mod_dur.empty() && mod_dur !=
"forever";
1190 return mod_dur == goal_dur;
1196 const unit_type* rebuild_from =
nullptr;
1200 for(
const auto& mod_name : ModificationTypes) {
1213 else if(rebuild_from ==
nullptr) {
1214 rebuild_from = &
type();
1222 if(rebuild_from !=
nullptr) {
1287 std::set<std::string> all_states =
states_;
1290 all_states.insert(state.first);
1295 if(all_states.count(
"undrainable") && all_states.count(
"unpoisonable") && all_states.count(
"unplagueable")) {
1296 all_states.insert(
"not_living");
1306 return get_state(known_boolean_state_id);
1310 if(state ==
"not_living") {
1355 set_state(known_boolean_state_id, value);
1360 if(state ==
"not_living") {
1376 if(ab.cfg[
"id"] == ability) {
1389 if(i->cfg[
"id"] == ability) {
1399 for(
const auto& a_ptr :
attacks_) {
1400 if(a_ptr->get_changed()) {
1410 auto write_subtag = [&](
const std::string& key,
const config& child)
1414 if(!child.empty()) {
1435 cfg.
add_child(
"special_note")[
"note"] = note;
1440 cfg[
"halo"] = *
halo_;
1467 cfg[
"side"] =
side_;
1480 cfg[
"role"] =
role_;
1483 for(
const std::string& state :
get_states()) {
1484 status_flags[state] =
true;
1489 write_subtag(
"status", status_flags);
1497 cfg[
"overlays"] =
"";
1499 cfg[
"name"] =
name_;
1504 cfg[
"canrecruit"] =
true;
1564 if(!advancement.empty()) {
1565 cfg.
add_child(
"advancement", advancement);
1593 return utils::holds_alternative<upkeep_loyal>(
upkeep_);
1603 if(!defense_abilities.
empty()) {
1613 if(!(cfg[
"active_on"].empty() || (attacker && cfg[
"active_on"] ==
"offense") || (!attacker && cfg[
"active_on"] ==
"defense"))) {
1617 const std::string& apply_to = cfg[
"apply_to"];
1618 if(!apply_to.empty()) {
1619 if(damage_name != apply_to) {
1620 if(apply_to.find(
',') != std::string::npos &&
1621 apply_to.find(damage_name) != std::string::npos) {
1622 const std::vector<std::string>& vals =
utils::split(apply_to);
1623 if(std::find(vals.begin(),vals.end(),damage_name) == vals.end()) {
1646 i = resistance_abilities.
erase(
i);
1652 if(!resistance_abilities.
empty()) {
1655 res = 100 - std::min<int>(
1657 resistance_abilities.
highest(
"max_value").first
1666 std::map<std::string,std::string> temp;
1672 std::ostringstream tooltip;
1677 tooltip <<
s << std::endl;
1681 temp[image] = tooltip.str();
1685 const std::string&
image = adv[
"image"];
1690 std::ostringstream tooltip;
1691 tooltip << temp[image];
1693 const std::string& tt = adv[
"description"];
1695 tooltip << tt << std::endl;
1698 temp[image] = tooltip.str();
1706 std::vector<std::pair<std::string, std::string>> temp;
1707 std::pair<std::string, std::string> icon;
1710 icon.first = adv[
"icon"].str();
1711 icon.second = adv[
"description"].str();
1713 for(
unsigned j = 0, j_count =
modification_count(
"advancement", adv[
"id"]); j < j_count; ++j) {
1714 temp.push_back(icon);
1723 std::vector<config> res;
1725 if(adv[
"strict_amla"].to_bool() && !
advances_to_.empty()) {
1734 if(
modification_count(
"advancement", adv[
"id"]) >=
static_cast<unsigned>(adv[
"max_times"].to_int(1))) {
1738 std::vector<std::string> temp_require =
utils::split(adv[
"require_amla"]);
1739 std::vector<std::string> temp_exclude =
utils::split(adv[
"exclude_amla"]);
1741 if(temp_require.empty() && temp_exclude.empty()) {
1746 std::sort(temp_require.begin(), temp_require.end());
1747 std::sort(temp_exclude.begin(), temp_exclude.end());
1749 std::vector<std::string> uniq_require, uniq_exclude;
1751 std::unique_copy(temp_require.begin(), temp_require.end(), std::back_inserter(uniq_require));
1752 std::unique_copy(temp_exclude.begin(), temp_exclude.end(), std::back_inserter(uniq_exclude));
1754 bool exclusion_found =
false;
1755 for(
const std::string&
s : uniq_exclude) {
1756 int max_num = std::count(temp_exclude.begin(), temp_exclude.end(),
s);
1758 if(mod_num >= max_num) {
1759 exclusion_found =
true;
1764 if(exclusion_found) {
1768 bool requirements_done =
true;
1769 for(
const std::string&
s : uniq_require) {
1770 int required_num = std::count(temp_require.begin(), temp_require.end(),
s);
1772 if(required_num > mod_num) {
1773 requirements_done =
false;
1778 if(requirements_done) {
1806 std::size_t res = 0;
1808 if(
item[
"id"] ==
id) {
1814 if(mod_type ==
"advancement") {
1822 "alignment",
"attack",
"defense",
"ellipse",
"experience",
"fearless",
1823 "halo",
"healthy",
"hitpoints",
"image_mod",
"jamming",
"jamming_costs",
1824 "loyal",
"max_attacks",
"max_experience",
"movement",
"movement_costs",
1825 "new_ability",
"new_advancement",
"new_animation",
"new_attack",
"overlay",
"profile",
1826 "recall_cost",
"remove_ability",
"remove_advancement",
"remove_attacks",
"resistance",
1827 "status",
"type",
"variation",
"vision",
"vision_costs",
"zoc" 1832 if(apply_to ==
"attack") {
1833 std::vector<t_string> attack_names;
1837 bool affected =
a->describe_modification(effect, &desc);
1838 if(affected && !desc.empty()) {
1839 attack_names.emplace_back(
a->name(),
"wesnoth-units");
1842 if(!attack_names.empty()) {
1845 symbols[
"effect_description"] = desc;
1846 return VGETTEXT(
"$attack_list|: $effect_description", symbols);
1848 }
else if(apply_to ==
"hitpoints") {
1849 const std::string& increase_total = effect[
"increase_total"];
1850 if(!increase_total.empty()) {
1852 "<span color=\"$color\">$number_or_percent</span> HP",
1853 {{
"number_or_percent",
utils::print_modifier(increase_total)}, {
"color", increase_total[0] ==
'-' ?
"red" :
"green"}});
1856 const std::string& increase = effect[
"increase"];
1857 if(increase.empty()) {
1860 if(apply_to ==
"movement") {
1862 "<span color=\"$color\">$number_or_percent</span> move",
1863 "<span color=\"$color\">$number_or_percent</span> moves",
1864 std::stoi(increase),
1865 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"red" :
"green"}});
1866 }
else if(apply_to ==
"vision") {
1868 "<span color=\"$color\">$number_or_percent</span> vision",
1869 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"red" :
"green"}});
1870 }
else if(apply_to ==
"jamming") {
1872 "<span color=\"$color\">$number_or_percent</span> jamming",
1873 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"red" :
"green"}});
1874 }
else if(apply_to ==
"max_experience") {
1877 "<span color=\"$color\">$number_or_percent</span> XP to advance",
1878 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"green" :
"red"}});
1879 }
else if(apply_to ==
"max_attacks") {
1881 "<span color=\"$color\">$number_or_percent</span> attack per turn",
1882 "<span color=\"$color\">$number_or_percent</span> attacks per turn",
1883 std::stoi(increase),
1884 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"red" :
"green"}});
1885 }
else if(apply_to ==
"recall_cost") {
1888 "<span color=\"$color\">$number_or_percent</span> cost to recall",
1889 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"green" :
"red"}});
1898 if(apply_to ==
"fearless") {
1901 }
else if(apply_to ==
"healthy") {
1904 }
else if(apply_to ==
"profile") {
1918 for(
const config&
c : cfg_range) {
1919 if(!
c[
"remove"].to_bool()) {
1929 }
else if(apply_to ==
"new_attack") {
1932 }
else if(apply_to ==
"remove_attacks") {
1935 return a->matches_filter(effect);
1939 }
else if(apply_to ==
"attack") {
1942 a->apply_modification(effect);
1944 }
else if(apply_to ==
"hitpoints") {
1946 const std::string& increase_hp = effect[
"increase"];
1947 const std::string& increase_total = effect[
"increase_total"];
1948 const std::string& set_hp = effect[
"set"];
1949 const std::string& set_total = effect[
"set_total"];
1952 const bool violate_max = effect[
"violate_maximum"].to_bool();
1954 if(!set_hp.empty()) {
1955 if(set_hp.back() ==
'%') {
1962 if(!set_total.empty()) {
1963 if(set_total.back() ==
'%') {
1970 if(!increase_total.empty()) {
1978 if(effect[
"heal_full"].to_bool()) {
1982 if(!increase_hp.empty()) {
1988 LOG_UT <<
"resetting hp to max" << std::endl;
1995 }
else if(apply_to ==
"movement") {
1996 const std::string& increase = effect[
"increase"];
1998 if(!increase.empty()) {
2007 }
else if(apply_to ==
"vision") {
2008 const std::string& increase = effect[
"increase"];
2010 if(!increase.empty()) {
2016 }
else if(apply_to ==
"jamming") {
2017 const std::string& increase = effect[
"increase"];
2019 if(!increase.empty()) {
2024 }
else if(apply_to ==
"experience") {
2025 const std::string& increase = effect[
"increase"];
2026 const std::string&
set = effect[
"set"];
2029 if(
set.back() ==
'%') {
2036 if(increase.empty() ==
false) {
2039 }
else if(apply_to ==
"max_experience") {
2040 const std::string& increase = effect[
"increase"];
2041 const std::string&
set = effect[
"set"];
2043 if(
set.empty() ==
false) {
2044 if(
set.back() ==
'%') {
2051 if(increase.empty() ==
false) {
2056 }
else if(apply_to ==
"status") {
2057 const std::string& add = effect[
"add"];
2058 const std::string&
remove = effect[
"remove"];
2065 for(
const std::string& to_remove :
utils::split(
remove))
2075 }
else if(apply_to ==
"zoc") {
2080 }
else if(apply_to ==
"new_ability") {
2081 if(
const config& ab_effect = effect.
child(
"abilities")) {
2091 }
else if(apply_to ==
"remove_ability") {
2092 if(
const config& ab_effect = effect.
child(
"abilities")) {
2097 }
else if(apply_to ==
"image_mod") {
2098 LOG_UT <<
"applying image_mod" << std::endl;
2099 std::string mod = effect[
"replace"];
2103 LOG_UT <<
"applying image_mod" << std::endl;
2104 mod = effect[
"add"].str();
2114 LOG_UT <<
"applying image_mod" << std::endl;
2115 }
else if(apply_to ==
"new_animation") {
2116 anim_comp_->apply_new_animation_effect(effect);
2117 }
else if(apply_to ==
"ellipse") {
2119 }
else if(apply_to ==
"halo") {
2121 }
else if(apply_to ==
"overlay") {
2122 const std::string& add = effect[
"add"];
2123 const std::string& replace = effect[
"replace"];
2124 const std::string&
remove = effect[
"remove"];
2131 if(!
remove.empty()) {
2136 if(add.empty() &&
remove.empty() && !replace.empty()) {
2139 }
else if(apply_to ==
"new_advancement") {
2140 const std::string& types = effect[
"types"];
2141 const bool replace = effect[
"replace"].to_bool(
false);
2144 if(!types.empty()) {
2149 std::copy(temp_advances.begin(), temp_advances.end(), std::back_inserter(
advances_to_));
2162 }
else if(apply_to ==
"remove_advancement") {
2163 const std::string& types = effect[
"types"];
2164 const std::string& amlas = effect[
"amlas"];
2169 for(
const std::string&
unit : temp_advances) {
2179 if(std::find(temp_advances.begin(), temp_advances.end(),
advancements_[
i][
"id"]) != temp_advances.end()) {
2183 }
else if(apply_to ==
"alignment") {
2185 if(new_align.parse(effect[
"set"])) {
2188 }
else if(apply_to ==
"max_attacks") {
2189 const std::string& increase = effect[
"increase"];
2191 if(!increase.empty()) {
2194 }
else if(apply_to ==
"recall_cost") {
2195 const std::string& increase = effect[
"increase"];
2196 const std::string&
set = effect[
"set"];
2200 if(
set.back() ==
'%') {
2201 recall_cost_ = lexical_cast_default<int>(
set)*recall_cost/100;
2207 if(!increase.empty()) {
2210 }
else if(effect[
"apply_to"] ==
"variation") {
2213 assert(base_type !=
nullptr);
2215 if(effect[
"heal_full"].to_bool(
false)) {
2218 }
else if(effect[
"apply_to"] ==
"type") {
2219 std::string prev_type = effect[
"prev_type"];
2220 if(prev_type.empty()) {
2223 const std::string& new_type_id = effect[
"name"];
2226 const bool heal_full = effect[
"heal_full"].to_bool(
false);
2233 WRN_UT <<
"unknown type= in [effect]apply_to=type, ignoring" << std::endl;
2240 bool generate_description = mod[
"generate_description"].to_bool(
true);
2242 if(no_add ==
false) {
2246 bool set_poisoned =
false;
2247 config type_effect, variation_effect;
2248 std::vector<t_string> effects_description;
2251 if(
const config& afilter = effect.
child(
"filter")) {
2257 const std::string& apply_to = effect[
"apply_to"];
2258 int times = effect[
"times"].to_int(1);
2261 if(effect[
"times"] ==
"per level") {
2271 if(apply_to ==
"type") {
2272 set_poisoned =
false;
2273 type_effect = effect;
2276 if(apply_to ==
"variation") {
2277 set_poisoned =
false;
2278 variation_effect = effect;
2282 std::string description_component;
2293 description += description_component;
2296 set_poisoned =
true;
2298 set_poisoned =
false;
2309 if(effect[
"times"] ==
"per level" && !times) {
2310 description =
VGETTEXT(
"$effect_description per level", {{
"effect_description", description}});
2313 if(!description.
empty()) {
2314 effects_description.push_back(description);
2318 if((!type_effect.
empty() || !variation_effect.
empty()) && no_add ==
false) {
2319 if(!type_effect.
empty()) {
2320 std::string description;
2327 effects_description.push_back(description);
2329 if(!variation_effect.
empty()) {
2330 std::string description;
2337 effects_description.push_back(description);
2347 const t_string& mod_description = mod[
"description"];
2348 if(!mod_description.
empty()) {
2349 description = mod_description;
2354 if(generate_description && !effects_description.empty()) {
2355 if(!mod_description.
empty()) {
2356 description +=
"\n";
2359 for(
const auto& desc_line : effects_description) {
2360 description += desc_line +
"\n";
2365 if(mod_type ==
"trait") {
2377 const t_string name = gender_specific_name.
empty() ? trait[
"name"] : gender_specific_name;
2411 DBG_UT <<
"unit::invisible called: id = " <<
id() <<
" loc = " << loc <<
" get_loc = " <<
get_location() << std::endl;
2428 return itor->second;
2433 static const std::string hides(
"hides");
2442 units_with_cache.push_back(
this);
2503 std::stringstream ss;
2521 std::string::size_type pos =
id_.find_last_of(
'-');
2522 if(pos != std::string::npos && pos+1 <
id_.size()
2523 &&
id_.find_first_not_of(
"0123456789", pos+1) == std::string::npos) {
2525 WRN_UT <<
"assigning new id to clone of generic unit " <<
id_ << std::endl;
2535 : u_(const_cast<
unit&>(u))
2552 DBG_UT <<
"The unit to be removed is not in the unit map." << std::endl;
2566 if(!image_mods_.empty()) {
2567 return formatter() <<
"~" << image_mods_ << TC_image_mods();
2570 return TC_image_mods();
2576 set_attr_changed(UA_ATTACKS);
2577 auto iter = std::find(attacks_.begin(), attacks_.end(), atk);
2578 if(iter == attacks_.end()) {
2581 attacks_.erase(iter);
2587 if(attacks_left_ == max_attacks_) {
2596 if(movement_left() == total_movement()) {
2597 set_state(STATE_NOT_MOVED,
true);
2600 set_movement(0,
true);
2612 anim_comp_->clear_haloes();
2617 appearance_changed_ =
true;
2618 anim_comp_->clear_haloes();
2624 if(upkeep.
empty()) {
2630 }
catch(std::invalid_argument&
e) {
2631 WRN_UT <<
"Found invalid upkeep=\"" << e.what() <<
"\" in a unit" << std::endl;
2643 changed_attributes_.reset();
2644 for(
const auto& a_ptr : attacks_) {
2645 a_ptr->set_changed(
false);
2655 u.
write(unit_config);
2657 const std::array<std::string, 22> main_keys {{
2664 "ignore_race_traits",
2665 "ignore_global_traits",
2682 for(
const std::string& main_key : main_keys) {
2683 wcfg[main_key] = unit_config[main_key];
2686 const std::array<std::string, 5> attack_keys {{
2697 for(
const std::string& attack_key : attack_keys) {
2698 child[attack_key] = att[attack_key];
2726 const std::array<std::string, 6> child_keys {{
2735 for(
const std::string& child_key : child_keys) {
int defense_modifier(const t_translation::terrain_code &terrain) const
Returns the defensive value of the indicated terrain.
bool empty() const
Tests for an attribute that either was never set or was set to "".
void remove()
Removes a tip.
const std::string & parent_id() const
The id of the original type from which this (variation) descended.
~unit_movement_resetter()
void set_movement(int moves, bool unit_action=false)
Set this unit's remaining movement to moves.
std::string apply_effect(const std::string &name, unit &u, const config &cfg, bool need_apply)
static DIRECTION parse_direction(const std::string &str)
std::vector< t_string > trait_descriptions_
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...
std::string format_conjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a conjunctive list.
static display * get_singleton()
Returns the display object if a display object exists.
void set_big_profile(const std::string &value)
const_all_children_itors all_children_range() const
In-order iteration over all children.
static state_t get_known_boolean_state_id(const std::string &state)
Convert a string status effect ID to a built-in status effect ID.
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.
virtual const std::vector< team > & teams() const override
vconfig child(const std::string &key) const
Returns a child of *this whose key is key.
std::map< std::string, t_string > string_map
void apply_modifications()
Re-apply all saved modifications.
void clear_children(T... keys)
void recursive_clear_value(config_key_type key)
The unit cannot be healed.
This class represents a single unit of a specific type.
const std::string & type_id() const
The id of this unit's type.
std::string big_profile() const
An optional profile image displays when this unit is 'speaking' via [message].
Interfaces for manipulating version numbers of engine, add-ons, etc.
bool generate_name() const
void append(const config &cfg)
Append data from another config object to this one.
std::string small_profile_
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
void advance_to(const unit_type &t, bool use_traits=false)
Advances this unit to another type.
void new_turn()
Refresh unit for the beginning of a turn.
The unit is petrified - it cannot move or be attacked.
void set_usage(const std::string &usage)
Sets this unit's usage.
const std::vector< config > & modification_advancements() const
The raw, unparsed data for modification advancements.
unit_id next_id()
returns id for unit that is created
iterator erase(const iterator &erase_it)
const std::string & id() const
config::const_child_itors events() const
Variant for storing WML attributes.
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
const std::vector< t_string > & special_notes() const
unit_race::GENDER gender_
const std::string & big_profile() const
unit_race::GENDER gender() const
The gender of this unit.
void check_types(const std::vector< std::string > &types) const
const std::vector< std::string > & advances_to() const
A vector of unit_type ids that this unit_type can advance to.
static game_config_view wrap(const config &cfg)
static id_manager & global_instance()
New lexcical_cast header.
bool has_attribute(config_key_type key) const
int hitpoints() const
The current number of hitpoints this unit has.
std::vector< t_string > trait_names_
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
const std::string & flag_rgb() const
unsigned child_count(config_key_type key) const
std::optional< std::string > ellipse_
child_itors child_range(config_key_type key)
config & variables()
Gets any user-defined variables this unit 'owns'.
void set_image_ellipse(const std::string &ellipse)
Set the unit's ellipse image.
void set_small_profile(const std::string &value)
The unit is a guardian - it won't move unless a target is sighted.
attribute_map::value_type attribute
void generate_traits(bool must_have_only=false)
Applies mandatory traits (e.g.
map_location interrupted_move_
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
const std::string & variation() const
The ID of the variation of this unit's type.
void clear_changed_attributes()
The unit is slowed - it moves slower and does less damage.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
std::optional< std::string > usage_
int experience_needed(bool with_acceleration=true) const
unit_type_data unit_types
std::unique_ptr< unit_animation_component > anim_comp_
The unit is poisoned - it loses health each turn.
const config & get_cfg() const
int resistance_against(const attack_type &attack) const
Returns the resistance against the indicated attack.
std::string absolute_image() const
The name of the file to game_display (used in menus).
std::vector< config > advancements_
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
const std::string & variation_id() const
The id of this variation; empty if it's a gender variation or a base unit.
void remove_attacks_ai()
Set the unit to have no attacks left for this turn.
UNIT_ALIGNMENT alignment_
void merge(const config &new_cfg, bool overwrite=true)
Merges the given config over the existing data, the config should have zero or more children named "m...
void write(config &cfg) const
Writes the movement type data to the provided config.
void set_facing(map_location::DIRECTION dir) const
The this unit's facing.
void set_interrupted_move(const map_location &interrupted_move)
Set the target location of the unit's interrupted move.
static int xp_to_advance(const std::string &s)
void set_total_movement(int value)
const_attr_itors attribute_range() const
A single unit type that the player may recruit.
void write(config &cfg, bool write_all=true) const
Serializes the current unit metadata values.
bool filter_base_matches(const config &cfg, int def)
std::vector< std::string > recruit_list_
std::vector< unit_ability >::iterator iterator
state_t
Built-in status effects known to the engine.
void set_level(int level)
Sets the current level of this unit.
std::bitset< num_bool_states > known_boolean_states_
const unit_type & type() const
This unit's type, accounting for gender and variation.
color_t hp_color() const
Color for this unit's current hitpoints.
unsigned int experience_to_advance() const
The number of experience points this unit needs to level up, or 0 if current XP > max XP...
Main entry points of multiplayer mode.
void heal(int amount)
Heal the unit.
void add_modification(const std::string &type, const config &modification, bool no_add=false)
Add a new modification to the unit.
int defense_modifier(const t_translation::terrain_code &terrain) const
The unit's defense on a given terrain.
static void clear_status_caches()
Clear this unit status cache for all units.
void set_advances_to(const std::vector< std::string > &advances_to)
Sets this unit's advancement options.
const_all_children_iterator ordered_end() const
child_list get_children(const std::string &key) const
std::vector< std::string > overlays_
This class stores all the data for a single 'side' (in game nomenclature).
void set_max_attacks(int value)
A small explanation about what's going on here: Each action has access to two game_info objects First...
static const std::string & leader_crown()
The path to the leader crown overlay.
static std::map< std::string, state_t > known_boolean_state_names_
std::vector< t_string > special_notes_
const std::vector< unit_race::GENDER > & genders() const
The returned vector will not be empty, provided this has been built to the HELP_INDEXED status...
const std::string & id() const
Gets this unit's id.
void swap(unit &)
Swap, for copy and swap idiom.
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
void set_state(const std::string &state, bool value)
Set whether the unit is affected by a status effect.
static const unit_race null_race
Dummy race used when a race is not yet known.
void set_underlying_id(n_unit::id_manager &id_manager)
Sets the internal ID.
const movetype & movement_type() const
void set_recruits(const std::vector< std::string > &recruits)
Sets the recruit list.
const t_string & type_name() const
The name of the unit in the current language setting.
bool get_attr_changed(UNIT_ATTRIBUTE attr) const
void new_scenario()
Refresh unit for the beginning of a new scenario.
void set_alignment(UNIT_ALIGNMENT alignment)
Sets the alignment of this unit.
filter_context * filter_con
void parse_upkeep(const config::attribute_value &upkeep)
const std::vector< std::string > advances_to_translated() const
Gets the names of the possible types this unit can advance to on level-up.
const std::string & usage() const
void set_max_experience(int value)
color_t xp_color() const
Color for this unit's XP.
std::string ellipse() const
static const std::set< std::string > effects
The set of applicable effects for movement types.
const t_string & name() const
Gets this unit's translatable display name.
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
int upkeep() const
Gets the amount of gold this unit costs a side per turn.
std::vector< std::string > advances_to_t
bool is_enemy(int n) const
bool get_attacks_changed() const
const std::string & id() const
The id for this unit_type.
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
map_display and display: classes which take care of displaying the map and game-data on the screen...
std::string default_anim_image() const
The default image to use for animation frames with no defined image.
std::optional< std::string > halo_
const std::vector< std::string > & overlays() const
Get the unit's overlay images.
boost::iterator_range< const_attribute_iterator > const_attr_itors
std::string small_profile() const
An optional profile image to display in Help.
The unit is uncovered - it was hiding but has been spotted.
void set_advancements(std::vector< config > advancements)
Sets the raw modification advancement option data.
bool mod_duration_match(const std::string &mod_dur, const std::string &goal_dur)
Determines if mod_dur "matches" goal_dur.
static color_t hp_color_max()
static void add_color_info(const game_config_view &v, bool build_defaults)
game_events::manager * game_events
all_children_iterator erase(const all_children_iterator &i)
void expire_modifications(const std::string &duration)
Clears those modifications whose duration has expired.
std::size_t modification_count(const std::string &type, const std::string &id) const
Count modifications of a particular type.
void set_attr_changed(UNIT_ATTRIBUTE attr)
t_string type_name_
The displayed name of this unit type.
const std::string & gender_string(unit_race::GENDER gender)
static std::string get_side_color_id(unsigned side)
double xp_bar_scaling() const
unit & mark_clone(bool is_temporary)
Mark this unit as clone so it can be inserted to unit_map.
bool loyal() const
Gets whether this unit is loyal - ie, it costs no upkeep.
Encapsulates the map of the game.
boost::iterator_range< const_child_iterator > const_child_itors
std::set< std::string > states_
bool invisible(const map_location &loc, bool see_all=true) const
const std::string & undead_variation() const
Info on the type of unit that the unit reanimates as.
const unit_race * race_
Never nullptr, but may point to the null race.
const std::string log_id() const
A variant on id() that is more descriptive, for use with message logging.
bool get_ability_bool(const std::string &tag_name, const map_location &loc) const
Checks whether this unit currently possesses or is affected by a given ability.
const unit_race * find_race(const std::string &) const
void remove_ability_by_id(const std::string &ability)
Removes a unit's abilities with a specific ID.
void set_image_halo(const std::string &halo)
Set the unit's halo image.
void generate_name()
Generates a random race-appropriate name if one has not already been provided.
void swap(unit &lhs, unit &rhs)
Implement non-member swap function for std::swap (calls unit::swap).
void set_undead_variation(const std::string &value)
The ID of the undead variation (ie, dwarf, swimmer) of this unit.
int max_hitpoints() const
The max number of hitpoints this unit can have.
Visitor helper class to fetch the appropriate upkeep value.
std::string get_checksum(const unit &u)
Gets a checksum for a unit.
static map_location::DIRECTION s
const config & abilities_cfg() const
void set_emit_zoc(bool val)
Sets the raw zone-of-control flag.
void add_trait_description(const config &trait, const t_string &description)
Register a trait's name and its description for the UI's use.
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
std::map< std::string, std::string > advancement_icons() const
Gets and image path and and associated description for each advancement option.
#define log_scope(description)
static std::string type()
int get_composite_value() const
DIRECTION
Valid directions which can be moved in our hexagonal world.
std::vector< std::string > advances_to_
const std::string & icon() const
void validate_side(int side)
std::vector< std::string > get_traits_list() const
Gets a list of the traits this unit currently has.
std::unique_ptr< unit_formula_manager > formula_man_
std::set< std::string > & encountered_units()
int get_random_int(int min, int max)
This helper method provides a random int from the underlying generator, using results of next_random...
void heal_fully()
Fully heal the unit, restoring it to max hitpoints.
n_unit::unit_id underlying_id_
void set_max_hitpoints(int value)
static int sort(lua_State *L)
const std::string & small_profile() const
unit_movement_resetter(const unit_movement_resetter &)=delete
rng * generator
This generator is automatically synced during synced context.
std::map< map_location, bool > invisibility_cache_
Hold the visibility status cache for a unit, when not uncovered.
std::shared_ptr< attack_type > attack_ptr
config & add_child(config_key_type key)
bool is_visible_to_team(const team &team, bool const see_all=true) const
Handling of system events.
const std::vector< std::string > & recruits() const
The type IDs of the other units this unit may recruit, if possible.
const std::string & flag_rgb() const
Get the source color palette to use when recoloring the unit's image.
std::string undead_variation_
const_all_children_iterator ordered_begin() const
config::const_child_itors possible_traits() const
const std::string & image() const
static const unit_type & get_unit_type(const std::string &type_id)
Converts a string ID to a unit_type.
const unit_type * type_
Never nullptr.
static unit_race::GENDER generate_gender(const unit_type &type, bool random_gender)
void init(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
void write_upkeep(config::attribute_value &upkeep) const
unsigned int num_traits() const
static const std::set< std::string > builtin_effects
n_unit::id_manager & unit_id_manager()
const unit_type & get_variation(const std::string &id) const
std::vector< std::string > split(const config_attribute_value &val)
int apply_modifier(const int number, const std::string &amount, const int minimum)
std::vector< config > get_modification_advances() const
Gets any non-typed advanced options set by modifications.
const map_location & get_location() const
The current map location this unit is at.
A variable-expanding proxy for the config class.
const unit_race * race() const
Never returns nullptr, but may point to the null race.
Functions to load and save images from/to disk.
void adjust_profile(std::string &profile)
const config & get_config() const
Standard logging facilities (interface).
bool can_advance() const
Checks whether this unit has any options to advance to.
void apply_builtin_effect(std::string type, const config &effect)
Apply a builtin effect to the unit.
auto apply_visitor(const V &visitor) const
Visitor support: Applies a visitor to the underlying variant.
t_string unit_description() const
A detailed description of this unit.
const unit_type & get_gender_unit_type(std::string gender) const
Returns a gendered variant of this unit_type.
static void check_id(std::string &id)
Validate the id argument.
game_lua_kernel * lua_kernel
std::string describe_builtin_effect(std::string type, const config &effect)
Construct a string describing a built-in effect.
double hp_bar_scaling() const
int total_movement() const
The maximum moves this unit has.
bool fogged(const map_location &loc) const
int side() const
The side this unit belongs to.
void end_turn()
Refresh unit for the end of a turn.
static color_t hp_color_impl(int hitpoints, int max_hitpoints)
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.
unit_ability_list get_abilities(const std::string &tag_name, const map_location &loc) const
Gets the unit's active abilities of a particular type if it were on a specified location.
const_attack_itors attacks() const
Visitor helper class to parse the upkeep value from a config.
int resistance_against(const std::string &damage_name, bool attacker, const map_location &loc, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr) const
The unit's resistance against a given damage type.
unit_ability_list get_abilities_weapons(const std::string &tag_name, const map_location &loc, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr) const
std::vector< vconfig > child_list
std::vector< std::pair< std::string, std::string > > amla_icons() const
Gets the image and description data for modification advancements.
t_string unit_description() const
Visitor helper struct to fetch the upkeep type flag if applicable, or the the value otherwise...
A config object defines a single node in a WML file, with access to child nodes.
bool resistance_filter_matches(const config &cfg, bool attacker, const std::string &damage_name, int res) const
std::shared_ptr< const attack_type > const_attack_ptr
ALIGNMENT alignment() const
std::string TC_image_mods() const
Constructs a recolor (RC) IPF string for this unit's team color.
void remove_movement_ai()
Sets the unit to have no moves left for this turn.
const std::set< std::string > get_states() const
Get the status effects currently affecting the unit.
const advances_to_t & advances_to() const
Gets the possible types this unit can advance to on level-up.
std::string print_modifier(const std::string &mod)
Add a "+" or replace the "-" par Unicode minus.
int recall_cost() const
How much gold it costs to recall this unit, or -1 if the side's default recall cost is used...
static std::string write_direction(DIRECTION dir)
void remove_child(config_key_type key, unsigned index)
bool would_be_discovered(const map_location &loc, int side_num, bool see_all=true)
Given a location and a side number, indicates whether an invisible unit of that side at that location...
static rng & default_instance()
void add_events(const config::const_child_itors &cfgs, const std::string &type=std::string())
static lg::log_domain log_unit("unit")
std::string::const_iterator iterator
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
std::bitset< UA_COUNT > changed_attributes_
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
bool has_ability_by_id(const std::string &ability) const
Check if the unit has an ability of a specific ID.
utils::string_map modification_descriptions_
config::const_child_itors advancements() const
std::string generate_name(GENDER gender) const
std::pair< int, map_location > highest(const std::string &key, int def=0) const
const std::string & default_variation() const
std::pair< std::string, unsigned > item
std::vector< std::string > parenthetical_split(const std::string &val, const char separator, const std::string &left, const std::string &right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...
map_location::DIRECTION facing_
bool remove_attack(attack_ptr atk)
Remove an attack from the unit.