64 #include <string_view> 69 #define DBG_UT LOG_STREAM(debug, log_unit) 70 #define LOG_UT LOG_STREAM(info, log_unit) 71 #define WRN_UT LOG_STREAM(warn, log_unit) 72 #define ERR_UT LOG_STREAM(err, log_unit) 77 const std::set<std::string_view> ModificationTypes {
"advancement",
"advance",
"trait",
"object" };
84 static std::vector<const unit*> units_with_cache;
86 static const std::string leader_crown_path =
"misc/leader-crown.png";
87 static const std::set<std::string_view> internalized_attrs {
156 auto cur_known = internalized_attrs.begin();
157 auto end_known = internalized_attrs.end();
159 while(cur_known != end_known) {
163 int comp = cur->first.compare(*cur_known);
165 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
178 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
190 if(type_id.empty()) {
193 std::string new_id = type_id;
202 const std::vector<unit_race::GENDER>& genders = type.
genders();
203 assert(genders.size() > 0);
205 if(random_gender ==
false || genders.size() == 1) {
206 return genders.front();
214 const std::string& gender = cfg[
"gender"];
215 if(!gender.empty()) {
224 :
std::enable_shared_from_this<
unit>()
226 , advances_to_(o.advances_to_)
228 , type_name_(o.type_name_)
232 , underlying_id_(o.underlying_id_)
233 , undead_variation_(o.undead_variation_)
234 , variation_(o.variation_)
235 , hit_points_(o.hit_points_)
236 , max_hit_points_(o.max_hit_points_)
237 , experience_(o.experience_)
238 , max_experience_(o.max_experience_)
240 , recall_cost_(o.recall_cost_)
241 , canrecruit_(o.canrecruit_)
242 , recruit_list_(o.recruit_list_)
243 , alignment_(o.alignment_)
244 , flag_rgb_(o.flag_rgb_)
245 , image_mods_(o.image_mods_)
246 , unrenamable_(o.unrenamable_)
250 , movement_(o.movement_)
251 , max_movement_(o.max_movement_)
253 , jamming_(o.jamming_)
254 , movement_type_(o.movement_type_)
255 , hold_position_(o.hold_position_)
256 , end_turn_(o.end_turn_)
257 , resting_(o.resting_)
258 , attacks_left_(o.attacks_left_)
259 , max_attacks_(o.max_attacks_)
261 , known_boolean_states_(o.known_boolean_states_)
262 , variables_(o.variables_)
264 , filter_recall_(o.filter_recall_)
265 , emit_zoc_(o.emit_zoc_)
266 , overlays_(o.overlays_)
268 , attacks_(o.attacks_)
270 , trait_names_(o.trait_names_)
271 , trait_descriptions_(o.trait_descriptions_)
272 , unit_value_(o.unit_value_)
274 , interrupted_move_(o.interrupted_move_)
275 , is_fearless_(o.is_fearless_)
276 , is_healthy_(o.is_healthy_)
277 , modification_descriptions_(o.modification_descriptions_)
280 , hp_bar_scaling_(o.hp_bar_scaling_)
281 , xp_bar_scaling_(o.xp_bar_scaling_)
282 , modifications_(o.modifications_)
283 , abilities_(o.abilities_)
284 , advancements_(o.advancements_)
285 , description_(o.description_)
286 , special_notes_(o.special_notes_)
289 , ellipse_(o.ellipse_)
290 , random_traits_(o.random_traits_)
291 , generate_name_(o.generate_name_)
293 , profile_(o.profile_)
294 , small_profile_(o.small_profile_)
295 , changed_attributes_(o.changed_attributes_)
296 , invisibility_cache_()
305 :
std::enable_shared_from_this<
unit>()
384 id_ = cfg[
"id"].str();
385 name_ = cfg[
"name"].t_str();
389 role_ = cfg[
"role"].str();
392 hidden_ = cfg[
"hidden"].to_bool(
false);
397 side_ = cfg[
"side"].to_int();
408 const vconfig& filter_recall = vcfg->
child(
"filter_recall");
409 if(!filter_recall.
null())
444 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.");
447 effect[
"apply_to"] =
"overlay";
448 effect[
"add"] = v->str();
524 if(temp_advances.size() == 1 && temp_advances.front() ==
"null") {
526 }
else if(temp_advances.size() >= 1 && !temp_advances.front().empty()) {
533 for(
config mai :
ai.child_range(
"micro_ai")) {
535 mai.add_child(
"filter")[
"id"] =
id();
536 mai[
"side"] =
side();
537 mai[
"action"] =
"add";
540 for(
config ca :
ai.child_range(
"candidate_action")) {
542 ca.add_child(
"filter_own")[
"id"] =
id();
544 ca.remove_attribute(
"sticky");
545 std::string stage =
"main_loop";
546 if(ca.has_attribute(
"stage")) {
547 stage = ca[
"stage"].str();
548 ca.remove_attribute(
"stage");
553 "path",
"stage[" + stage +
"].candidate_action[]",
554 "candidate_action", ca,
567 for(
const config&
c : cfg_range) {
576 for(
const config&
c : cfg_range) {
622 if(
const config& status_flags = cfg.
child(
"status")) {
624 if(st.second.to_bool()) {
630 if(cfg[
"ai_special"] ==
"guardian") {
651 resting_ = cfg[
"resting"].to_bool();
656 if(!cfg[
"recall_cost"].
blank()) {
670 for(
const auto& anim :
anim_comp_->animations_) {
671 std::cout << anim.debug() << std::endl;
678 for(
auto& u : units_with_cache) {
679 u->clear_visibility_cache();
682 units_with_cache.clear();
716 auto itor = std::find(units_with_cache.begin(), units_with_cache.end(),
this);
718 if(itor != units_with_cache.end()) {
719 units_with_cache.erase(itor);
721 }
catch(
const std::exception &
e) {
722 ERR_UT <<
"Caught exception when destroying unit: " << e.what();
806 LOG_UT <<
"Generating a trait for unit type " <<
type().
log_id() <<
" with must_have_only " << must_have_only;
814 const std::string& tid =
t[
"id"];
815 bool already =
false;
816 for(
const config& mod : current_traits) {
817 if(mod[
"id"] == tid) {
826 const std::string& avl =
t[
"availability"];
827 if(avl ==
"musthave") {
838 std::vector<const config*> candidate_traits;
839 std::vector<std::string> temp_require_traits;
840 std::vector<std::string> temp_exclude_traits;
844 int nb_traits = current_traits.size();
846 for(; nb_traits < max_traits; ++nb_traits)
849 candidate_traits.clear();
852 const std::string& tid =
t[
"id"];
853 bool already =
false;
854 for(
const config& mod : current_traits) {
855 if(mod[
"id"] == tid) {
870 for(
const config& mod : current_traits) {
871 if (mod[
"exclude_traits"] !=
"") {
873 temp_exclude_traits.push_back(
c);
879 bool trait_req_met =
true;
880 for(
const std::string&
s : temp_require_traits) {
881 bool has_trait =
false;
882 for(
const config& mod : current_traits) {
887 trait_req_met =
false;
896 bool trait_exc_met =
true;
898 for(
const std::string&
s : temp_exclude_traits) {
899 bool has_exclusionary_trait =
false;
900 for(
const config& mod : current_traits) {
902 has_exclusionary_trait =
true;
905 has_exclusionary_trait =
true;
907 if(has_exclusionary_trait) {
908 trait_exc_met =
false;
916 const std::string& avl =
t[
"availability"];
919 if(!must_have_only && (!
can_recruit() || avl ==
"any")) {
920 candidate_traits.push_back(&
t);
924 if(candidate_traits.empty()) {
930 candidate_traits.erase(candidate_traits.begin() + num);
939 std::vector<std::string> res;
945 res.push_back(mod[
"id"]);
980 if(!new_type.
usage().empty()) {
985 if(!new_type.
ellipse().empty()) {
1030 return std::make_shared<attack_type>(atk);
1117 return leader_crown_path;
1127 double unit_energy = 0.0;
1128 color_t energy_color {0,0,0,255};
1130 if(max_hitpoints > 0) {
1131 unit_energy =
static_cast<double>(
hitpoints)/static_cast<double>(max_hitpoints);
1134 if(1.0 == unit_energy) {
1135 energy_color.r = 33;
1136 energy_color.g = 225;
1138 }
else if(unit_energy > 1.0) {
1139 energy_color.r = 100;
1140 energy_color.g = 255;
1141 energy_color.b = 100;
1142 }
else if(unit_energy >= 0.75) {
1143 energy_color.r = 170;
1144 energy_color.g = 255;
1146 }
else if(unit_energy >= 0.5) {
1147 energy_color.r = 255;
1148 energy_color.g = 175;
1150 }
else if(unit_energy >= 0.25) {
1151 energy_color.r = 255;
1152 energy_color.g = 155;
1155 energy_color.r = 255;
1160 return energy_color;
1180 const color_t near_advance_color {255,255,255,255};
1181 const color_t mid_advance_color {150,255,255,255};
1182 const color_t far_advance_color {0,205,205,255};
1183 const color_t normal_color {0,160,225,255};
1184 const color_t near_amla_color {225,0,255,255};
1185 const color_t mid_amla_color {169,30,255,255};
1186 const color_t far_amla_color {139,0,237,255};
1187 const color_t amla_color {170,0,255,255};
1196 color=near_advance_color;
1197 }
else if(mid_advance){
1198 color=mid_advance_color;
1199 }
else if(far_advance){
1200 color=far_advance_color;
1202 }
else if(has_amla){
1204 color=near_amla_color;
1205 }
else if(mid_advance){
1206 color=mid_amla_color;
1207 }
else if(far_advance){
1208 color=far_amla_color;
1219 bool major_amla =
false;
1220 bool has_amla =
false;
1222 major_amla |= adv[
"major_amla"].to_bool();
1237 std::vector<std::string> result;
1240 result.push_back(adv_type->type_name());
1242 WRN_UT <<
"unknown unit in advances_to list of type " 1275 if(goal_dur.empty()) {
1277 return !mod_dur.empty() && mod_dur !=
"forever";
1280 return mod_dur == goal_dur;
1286 const unit_type* rebuild_from =
nullptr;
1290 for(
const auto& mod_name : ModificationTypes) {
1303 else if(rebuild_from ==
nullptr) {
1304 rebuild_from = &
type();
1312 if(rebuild_from !=
nullptr) {
1377 std::set<std::string> all_states =
states_;
1380 all_states.insert(state.first);
1385 if(all_states.count(
"undrainable") && all_states.count(
"unpoisonable") && all_states.count(
"unplagueable")) {
1386 all_states.insert(
"not_living");
1396 return get_state(known_boolean_state_id);
1400 if(state ==
"not_living") {
1446 set_state(known_boolean_state_id, value);
1451 if(state ==
"not_living") {
1467 if(ab.cfg[
"id"] == ability) {
1480 if(i->cfg[
"id"] == ability) {
1490 for(
const auto& a_ptr :
attacks_) {
1491 if(a_ptr->get_changed()) {
1501 auto write_subtag = [&](
const std::string& key,
const config& child)
1505 if(!child.empty()) {
1526 cfg.
add_child(
"special_note")[
"note"] = note;
1531 cfg[
"halo"] = *
halo_;
1558 cfg[
"side"] =
side_;
1571 cfg[
"role"] =
role_;
1574 for(
const std::string& state :
get_states()) {
1575 status_flags[state] =
true;
1580 write_subtag(
"status", status_flags);
1588 cfg[
"overlays"] =
"";
1590 cfg[
"name"] =
name_;
1595 cfg[
"canrecruit"] =
true;
1655 if(!advancement.empty()) {
1656 cfg.
add_child(
"advancement", advancement);
1684 return utils::holds_alternative<upkeep_loyal>(
upkeep_);
1694 if(!defense_abilities.
empty()) {
1704 if(!(cfg[
"active_on"].empty() || (attacker && cfg[
"active_on"] ==
"offense") || (!attacker && cfg[
"active_on"] ==
"defense"))) {
1708 const std::string& apply_to = cfg[
"apply_to"];
1709 if(!apply_to.empty()) {
1710 if(damage_name != apply_to) {
1711 if(apply_to.find(
',') != std::string::npos &&
1712 apply_to.find(damage_name) != std::string::npos) {
1713 const std::vector<std::string>& vals =
utils::split(apply_to);
1714 if(std::find(vals.begin(),vals.end(),damage_name) == vals.end()) {
1739 if(!resistance_abilities.
empty()) {
1742 res = 100 - std::min<int>(
1744 resistance_abilities.
highest(
"max_value").first
1753 std::map<std::string,std::string> temp;
1764 tooltip <<
s << std::endl;
1768 temp[image] = tooltip.str();
1772 const std::string&
image = adv[
"image"];
1778 tooltip << temp[image];
1780 const std::string& tt = adv[
"description"];
1782 tooltip << tt << std::endl;
1785 temp[image] = tooltip.str();
1793 std::vector<std::pair<std::string, std::string>> temp;
1794 std::pair<std::string, std::string> icon;
1797 icon.first = adv[
"icon"].str();
1798 icon.second = adv[
"description"].str();
1800 for(
unsigned j = 0, j_count =
modification_count(
"advancement", adv[
"id"]); j < j_count; ++j) {
1801 temp.push_back(icon);
1810 std::vector<config> res;
1812 if(adv[
"strict_amla"].to_bool() && !
advances_to_.empty()) {
1821 if(
modification_count(
"advancement", adv[
"id"]) >=
static_cast<unsigned>(adv[
"max_times"].to_int(1))) {
1825 std::vector<std::string> temp_require =
utils::split(adv[
"require_amla"]);
1826 std::vector<std::string> temp_exclude =
utils::split(adv[
"exclude_amla"]);
1828 if(temp_require.empty() && temp_exclude.empty()) {
1833 std::sort(temp_require.begin(), temp_require.end());
1834 std::sort(temp_exclude.begin(), temp_exclude.end());
1836 std::vector<std::string> uniq_require, uniq_exclude;
1838 std::unique_copy(temp_require.begin(), temp_require.end(), std::back_inserter(uniq_require));
1839 std::unique_copy(temp_exclude.begin(), temp_exclude.end(), std::back_inserter(uniq_exclude));
1841 bool exclusion_found =
false;
1842 for(
const std::string&
s : uniq_exclude) {
1843 int max_num = std::count(temp_exclude.begin(), temp_exclude.end(),
s);
1845 if(mod_num >= max_num) {
1846 exclusion_found =
true;
1851 if(exclusion_found) {
1855 bool requirements_done =
true;
1856 for(
const std::string&
s : uniq_require) {
1857 int required_num = std::count(temp_require.begin(), temp_require.end(),
s);
1859 if(required_num > mod_num) {
1860 requirements_done =
false;
1865 if(requirements_done) {
1893 std::size_t res = 0;
1895 if(
item[
"id"] ==
id) {
1901 if(mod_type ==
"advancement") {
1909 "alignment",
"attack",
"defense",
"ellipse",
"experience",
"fearless",
1910 "halo",
"healthy",
"hitpoints",
"image_mod",
"jamming",
"jamming_costs",
1911 "loyal",
"max_attacks",
"max_experience",
"movement",
"movement_costs",
1912 "new_ability",
"new_advancement",
"new_animation",
"new_attack",
"overlay",
"profile",
1913 "recall_cost",
"remove_ability",
"remove_advancement",
"remove_attacks",
"resistance",
1914 "status",
"type",
"variation",
"vision",
"vision_costs",
"zoc" 1919 if(apply_to ==
"attack") {
1920 std::vector<t_string> attack_names;
1924 bool affected =
a->describe_modification(effect, &desc);
1925 if(affected && !desc.empty()) {
1926 attack_names.emplace_back(
a->name(),
"wesnoth-units");
1929 if(!attack_names.empty()) {
1932 symbols[
"effect_description"] = desc;
1933 return VGETTEXT(
"$attack_list|: $effect_description", symbols);
1935 }
else if(apply_to ==
"hitpoints") {
1936 const std::string& increase_total = effect[
"increase_total"];
1937 if(!increase_total.empty()) {
1939 "<span color=\"$color\">$number_or_percent</span> HP",
1940 {{
"number_or_percent",
utils::print_modifier(increase_total)}, {
"color", increase_total[0] ==
'-' ?
"#f00" :
"#0f0"}});
1943 const std::string& increase = effect[
"increase"];
1944 if(increase.empty()) {
1947 if(apply_to ==
"movement") {
1949 "<span color=\"$color\">$number_or_percent</span> move",
1950 "<span color=\"$color\">$number_or_percent</span> moves",
1951 std::stoi(increase),
1952 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
1953 }
else if(apply_to ==
"vision") {
1955 "<span color=\"$color\">$number_or_percent</span> vision",
1956 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
1957 }
else if(apply_to ==
"jamming") {
1959 "<span color=\"$color\">$number_or_percent</span> jamming",
1960 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
1961 }
else if(apply_to ==
"max_experience") {
1964 "<span color=\"$color\">$number_or_percent</span> XP to advance",
1965 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
1966 }
else if(apply_to ==
"max_attacks") {
1968 "<span color=\"$color\">$number_or_percent</span> attack per turn",
1969 "<span color=\"$color\">$number_or_percent</span> attacks per turn",
1970 std::stoi(increase),
1971 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
1972 }
else if(apply_to ==
"recall_cost") {
1975 "<span color=\"$color\">$number_or_percent</span> cost to recall",
1976 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
1985 if(apply_to ==
"fearless") {
1988 }
else if(apply_to ==
"healthy") {
1991 }
else if(apply_to ==
"profile") {
2005 for(
const config&
c : cfg_range) {
2006 if(!
c[
"remove"].to_bool()) {
2016 }
else if(apply_to ==
"new_attack") {
2019 }
else if(apply_to ==
"remove_attacks") {
2022 return a->matches_filter(effect);
2026 }
else if(apply_to ==
"attack") {
2029 a->apply_modification(effect);
2031 }
else if(apply_to ==
"hitpoints") {
2033 const std::string& increase_hp = effect[
"increase"];
2034 const std::string& increase_total = effect[
"increase_total"];
2035 const std::string& set_hp = effect[
"set"];
2036 const std::string& set_total = effect[
"set_total"];
2039 const bool violate_max = effect[
"violate_maximum"].to_bool();
2041 if(!set_hp.empty()) {
2042 if(set_hp.back() ==
'%') {
2043 hit_points_ = lexical_cast_default<int>(set_hp)*max_hit_points_/100;
2049 if(!set_total.empty()) {
2050 if(set_total.back() ==
'%') {
2057 if(!increase_total.empty()) {
2062 if(max_hit_points_ < 1)
2065 if(effect[
"heal_full"].to_bool()) {
2069 if(!increase_hp.empty()) {
2074 if(
hit_points_ > max_hit_points_ && !violate_max) {
2075 LOG_UT <<
"resetting hp to max";
2082 }
else if(apply_to ==
"movement") {
2083 const bool apply_to_vision = effect[
"apply_to_vision"].to_bool(
true);
2092 const std::string& increase = effect[
"increase"];
2093 if(!increase.empty()) {
2103 if(apply_to_vision) {
2106 }
else if(apply_to ==
"vision") {
2112 const std::string& increase = effect[
"increase"];
2113 if(!increase.empty()) {
2118 }
else if(apply_to ==
"jamming") {
2119 const std::string& increase = effect[
"increase"];
2121 if(!increase.empty()) {
2126 }
else if(apply_to ==
"experience") {
2127 const std::string& increase = effect[
"increase"];
2128 const std::string&
set = effect[
"set"];
2131 if(
set.back() ==
'%') {
2138 if(increase.empty() ==
false) {
2141 }
else if(apply_to ==
"max_experience") {
2142 const std::string& increase = effect[
"increase"];
2143 const std::string&
set = effect[
"set"];
2145 if(
set.empty() ==
false) {
2146 if(
set.back() ==
'%') {
2153 if(increase.empty() ==
false) {
2158 }
else if(apply_to ==
"status") {
2159 const std::string& add = effect[
"add"];
2160 const std::string&
remove = effect[
"remove"];
2167 for(
const std::string& to_remove :
utils::split(
remove))
2177 }
else if(apply_to ==
"zoc") {
2182 }
else if(apply_to ==
"new_ability") {
2183 if(
const config& ab_effect = effect.
child(
"abilities")) {
2193 }
else if(apply_to ==
"remove_ability") {
2194 if(
const config& ab_effect = effect.
child(
"abilities")) {
2199 }
else if(apply_to ==
"image_mod") {
2200 LOG_UT <<
"applying image_mod";
2201 std::string mod = effect[
"replace"];
2205 LOG_UT <<
"applying image_mod";
2206 mod = effect[
"add"].str();
2216 LOG_UT <<
"applying image_mod";
2217 }
else if(apply_to ==
"new_animation") {
2218 anim_comp_->apply_new_animation_effect(effect);
2219 }
else if(apply_to ==
"ellipse") {
2221 }
else if(apply_to ==
"halo") {
2223 }
else if(apply_to ==
"overlay") {
2224 const std::string& add = effect[
"add"];
2225 const std::string& replace = effect[
"replace"];
2226 const std::string&
remove = effect[
"remove"];
2233 if(!
remove.empty()) {
2238 if(add.empty() &&
remove.empty() && !replace.empty()) {
2241 }
else if(apply_to ==
"new_advancement") {
2242 const std::string& types = effect[
"types"];
2243 const bool replace = effect[
"replace"].to_bool(
false);
2246 if(!types.empty()) {
2251 std::copy(temp_advances.begin(), temp_advances.end(), std::back_inserter(
advances_to_));
2264 }
else if(apply_to ==
"remove_advancement") {
2265 const std::string& types = effect[
"types"];
2266 const std::string& amlas = effect[
"amlas"];
2271 for(
const std::string&
unit : temp_advances) {
2281 if(std::find(temp_advances.begin(), temp_advances.end(),
advancements_[
i][
"id"]) != temp_advances.end()) {
2285 }
else if(apply_to ==
"alignment") {
2290 }
else if(apply_to ==
"max_attacks") {
2291 const std::string& increase = effect[
"increase"];
2293 if(!increase.empty()) {
2296 }
else if(apply_to ==
"recall_cost") {
2297 const std::string& increase = effect[
"increase"];
2298 const std::string&
set = effect[
"set"];
2302 if(
set.back() ==
'%') {
2303 recall_cost_ = lexical_cast_default<int>(
set)*recall_cost/100;
2309 if(!increase.empty()) {
2312 }
else if(effect[
"apply_to"] ==
"variation") {
2314 assert(base_type !=
nullptr);
2315 const std::string& variation_id = effect[
"name"];
2319 if(effect[
"heal_full"].to_bool(
false)) {
2323 WRN_UT <<
"unknown variation '" << variation_id <<
"' (name=) in [effect]apply_to=variation, ignoring";
2325 }
else if(effect[
"apply_to"] ==
"type") {
2326 std::string prev_type = effect[
"prev_type"];
2327 if(prev_type.empty()) {
2330 const std::string& new_type_id = effect[
"name"];
2335 if(effect[
"heal_full"].to_bool(
false)) {
2339 WRN_UT <<
"unknown type '" << new_type_id <<
"' (name=) in [effect]apply_to=type, ignoring";
2346 bool generate_description = mod[
"generate_description"].to_bool(
true);
2348 if(no_add ==
false) {
2352 bool set_poisoned =
false;
2353 config type_effect, variation_effect;
2354 std::vector<t_string> effects_description;
2357 if(
const config& afilter = effect.
child(
"filter")) {
2363 const std::string& apply_to = effect[
"apply_to"];
2364 int times = effect[
"times"].to_int(1);
2367 if(effect[
"times"] ==
"per level") {
2377 if(apply_to ==
"type") {
2378 set_poisoned =
false;
2379 type_effect = effect;
2382 if(apply_to ==
"variation") {
2383 set_poisoned =
false;
2384 variation_effect = effect;
2388 std::string description_component;
2399 description += description_component;
2402 set_poisoned =
true;
2404 set_poisoned =
false;
2415 if(effect[
"times"] ==
"per level" && !times) {
2416 description =
VGETTEXT(
"$effect_description per level", {{
"effect_description", description}});
2419 if(!description.
empty()) {
2420 effects_description.push_back(description);
2424 if((!type_effect.
empty() || !variation_effect.
empty()) && no_add ==
false) {
2425 if(!type_effect.
empty()) {
2426 std::string description;
2433 effects_description.push_back(description);
2435 if(!variation_effect.
empty()) {
2436 std::string description;
2443 effects_description.push_back(description);
2453 const t_string& mod_description = mod[
"description"];
2454 if(!mod_description.
empty()) {
2455 description = mod_description;
2460 if(generate_description && !effects_description.empty()) {
2461 if(!mod_description.
empty()) {
2462 description +=
"\n";
2465 for(
const auto& desc_line : effects_description) {
2466 description += desc_line +
"\n";
2471 if(mod_type ==
"trait") {
2483 const t_string name = gender_specific_name.
empty() ? trait[
"name"] : gender_specific_name;
2517 DBG_UT <<
"unit::invisible called: id = " <<
id() <<
" loc = " << loc <<
" get_loc = " <<
get_location();
2534 return itor->second;
2539 static const std::string hides(
"hides");
2548 units_with_cache.push_back(
this);
2609 std::stringstream ss;
2627 std::string::size_type pos =
id_.find_last_of(
'-');
2628 if(pos != std::string::npos && pos+1 <
id_.size()
2629 &&
id_.find_first_not_of(
"0123456789", pos+1) == std::string::npos) {
2631 WRN_UT <<
"assigning new id to clone of generic unit " <<
id_;
2641 : u_(const_cast<
unit&>(u))
2658 DBG_UT <<
"The unit to be removed is not in the unit map.";
2674 if(!image_mods_.empty()) {
2675 return formatter() <<
"~" << image_mods_ << TC_image_mods();
2678 return TC_image_mods();
2684 set_attr_changed(UA_ATTACKS);
2685 auto iter = std::find(attacks_.begin(), attacks_.end(), atk);
2686 if(iter == attacks_.end()) {
2689 attacks_.erase(iter);
2695 if(attacks_left_ == max_attacks_) {
2704 if(movement_left() == total_movement()) {
2705 set_state(STATE_NOT_MOVED,
true);
2708 set_movement(0,
true);
2721 anim_comp_->clear_haloes();
2726 appearance_changed_ =
true;
2727 anim_comp_->clear_haloes();
2733 if(upkeep.
empty()) {
2739 }
catch(std::invalid_argument&
e) {
2740 WRN_UT <<
"Found invalid upkeep=\"" << e.what() <<
"\" in a unit";
2752 changed_attributes_.reset();
2753 for(
const auto& a_ptr : attacks_) {
2754 a_ptr->set_changed(
false);
2768 u.
write(unit_config);
2770 static const std::set<std::string_view> main_keys {
2777 "ignore_race_traits",
2778 "ignore_global_traits",
2795 for(
const std::string_view& main_key : main_keys) {
2796 wcfg[main_key] = unit_config[main_key];
2799 static const std::set<std::string_view> attack_keys {
2810 for(
const std::string_view& attack_key : attack_keys) {
2811 child[attack_key] = att[attack_key];
2844 static const std::set<std::string_view> child_keys {
2853 for(
const std::string_view& 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
const config * ability_cfg
The contents of the ability tag, never nullptr.
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.
static manager & get_singleton()
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
std::vector< t_string > combine_special_notes(const std::vector< t_string > direct, const config &abilities, const_attack_itors attacks, const movetype &mt)
Common logic for unit_type::special_notes() and unit::special_notes().
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.
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.
unit_alignments::type alignment_
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)
To set the size of known_boolean_states_.
attribute_map::value_type attribute
void generate_traits(bool must_have_only=false)
Applies mandatory traits (e.g.
map_location interrupted_move_
The unit is a guardian - it won't move unless a target is sighted.
unit_checksum_version
Optional parameter for get_checksum to use the algorithm of an older version of Wesnoth, thus preventing spurious OOS warnings while watching old replays.
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::string get_unknown_exception_type()
Utility function for finding the type of thing caught with catch(...).
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.
void append_children(const config &cfg)
Adds children from cfg.
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 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.
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_
void append_active_ai_for_side(ai::side_number side, const config &cfg)
Appends AI parameters to active AI of the given side.
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.
creating intitial [unit]s, executing toplevel [lua] etc.
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...
Data typedef for unit_ability_list.
static const std::string & leader_crown()
The path to the leader crown overlay.
static std::map< std::string, state_t > known_boolean_state_names_
Included some of the flavortext from weapon specials.
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.
const config & abilities() const
void set_underlying_id(n_unit::id_manager &id_manager)
Sets the internal ID.
const movetype & movement_type() const
unit_alignments::type alignment() 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.
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.
unsigned all_children_count() const
std::string ellipse() const
static const std::set< std::string > effects
The set of applicable effects for movement types.
void set_alignment(unit_alignments::type alignment)
Sets the alignment of this unit.
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)
The basic class for representing 8-bit RGB or RGBA colour values.
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.
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands...
std::vector< t_string > unit_special_notes() const
The unit's special notes.
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 get_checksum(const unit &u, backwards_compatibility::unit_checksum_version version)
Gets a checksum for a unit.
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 erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
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.
static constexpr std::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
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_
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type=std::string())
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)
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 write(config &cfg, bool include_notes) const
Writes the movement type data to the provided config.
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
std::vector< t_string > direct_special_notes() const
Returns only the notes defined by [unit_type][special_note] tags, excluding any that would be found f...
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
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)
bool has_variation(const std::string &variation_id) const
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()
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::string tooltip
Shown when hovering over an entry in the filter's drop-down list.
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.
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.