57 #include <string_view>
62 #define DBG_UT LOG_STREAM(debug, log_unit)
63 #define LOG_UT LOG_STREAM(info, log_unit)
64 #define WRN_UT LOG_STREAM(warn, log_unit)
65 #define ERR_UT LOG_STREAM(err, log_unit)
70 const std::set<std::string_view> ModificationTypes {
"advancement",
"advance",
"trait",
"object" };
77 static std::vector<const unit*> units_with_cache;
79 static const std::string leader_crown_path =
"misc/leader-crown.png";
80 static const std::set<std::string_view> internalized_attrs {
151 auto cur_known = internalized_attrs.begin();
152 auto end_known = internalized_attrs.end();
154 while(cur_known != end_known) {
158 int comp = cur->first.compare(*cur_known);
160 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
173 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
178 auto stats_storage_resetter(
unit& u,
bool clamp =
false)
215 if(type_id.empty()) {
218 std::string new_id = type_id;
227 const std::vector<unit_race::GENDER>& genders =
type.genders();
228 assert(genders.size() > 0);
230 if(random_gender ==
false || genders.size() == 1) {
231 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 , trait_nonhidden_ids_(o.trait_nonhidden_ids_)
298 , unit_value_(o.unit_value_)
300 , interrupted_move_(o.interrupted_move_)
301 , is_fearless_(o.is_fearless_)
302 , is_healthy_(o.is_healthy_)
303 , modification_descriptions_(o.modification_descriptions_)
306 , hp_bar_scaling_(o.hp_bar_scaling_)
307 , xp_bar_scaling_(o.xp_bar_scaling_)
308 , modifications_(o.modifications_)
309 , abilities_(o.abilities_)
310 , advancements_(o.advancements_)
311 , description_(o.description_)
312 , special_notes_(o.special_notes_)
315 , ellipse_(o.ellipse_)
316 , random_traits_(o.random_traits_)
317 , generate_name_(o.generate_name_)
319 , profile_(o.profile_)
320 , small_profile_(o.small_profile_)
321 , changed_attributes_(o.changed_attributes_)
322 , invisibility_cache_()
331 : std::enable_shared_from_this<
unit>()
340 , undead_variation_()
353 , unrenamable_(false)
362 , hold_position_(false)
368 , known_boolean_states_()
378 , trait_descriptions_()
379 , trait_nonhidden_ids_()
382 , interrupted_move_()
383 , is_fearless_(false)
385 , modification_descriptions_()
398 , random_traits_(true)
399 , generate_name_(true)
401 , changed_attributes_(0)
402 , invisibility_cache_()
411 id_ = cfg[
"id"].str();
416 role_ = cfg[
"role"].str();
419 hidden_ = cfg[
"hidden"].to_bool(
false);
424 side_ = cfg[
"side"].to_int();
435 const vconfig& filter_recall = vcfg->
child(
"filter_recall");
436 if(!filter_recall.
null())
444 for(
const vconfig& abilities_tag : abilities_tags) {
445 for(
const auto& [key, child] : abilities_tag.all_ordered()) {
447 for(
const vconfig& ability_event : ability_events) {
455 for(
const vconfig& specials_tag : specials_tags) {
456 for(
const auto& [key, child] : specials_tag.all_ordered()) {
458 for(
const vconfig& special_event : special_events) {
479 for(
const auto [key, special] : specials.all_children_view()) {
508 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.");
511 effect[
"apply_to"] =
"overlay";
512 effect[
"add"] = v->str();
588 if(temp_advances.size() == 1 && temp_advances.front() ==
"null") {
590 }
else if(temp_advances.size() >= 1 && !temp_advances.front().empty()) {
597 for(
config mai :
ai->child_range(
"micro_ai")) {
599 mai.add_child(
"filter")[
"id"] =
id();
600 mai[
"side"] =
side();
601 mai[
"action"] =
"add";
604 for(
config ca :
ai->child_range(
"candidate_action")) {
606 ca.add_child(
"filter_own")[
"id"] =
id();
608 ca.remove_attribute(
"sticky");
609 std::string stage =
"main_loop";
610 if(ca.has_attribute(
"stage")) {
611 stage = ca[
"stage"].str();
612 ca.remove_attribute(
"stage");
617 "path",
"stage[" + stage +
"].candidate_action[]",
618 "candidate_action", ca,
631 for(
const config&
c : cfg_range) {
640 for(
const config&
c : cfg_range) {
687 for(
const auto& [key, value] : status_flags->attribute_range()) {
688 if(value.to_bool()) {
694 if(cfg[
"ai_special"] ==
"guardian") {
715 resting_ = cfg[
"resting"].to_bool();
720 if(!cfg[
"recall_cost"].
blank()) {
734 for(
const auto& anim :
anim_comp_->animations_) {
735 std::cout << anim.debug() << std::endl;
742 for(
auto& u : units_with_cache) {
743 u->clear_visibility_cache();
746 units_with_cache.clear();
756 facing_ = get_random_direction();
780 auto itor = std::find(units_with_cache.begin(), units_with_cache.end(),
this);
782 if(itor != units_with_cache.end()) {
783 units_with_cache.erase(itor);
785 }
catch(
const std::exception &
e) {
786 ERR_UT <<
"Caught exception when destroying unit: " <<
e.what();
803 LOG_UT <<
"Generating a trait for unit type " <<
type().
log_id() <<
" with must_have_only " << must_have_only;
811 const std::string& tid =
t[
"id"];
812 bool already =
false;
813 for(
const config& mod : current_traits) {
814 if(mod[
"id"] == tid) {
823 const std::string& avl =
t[
"availability"];
824 if(avl ==
"musthave") {
835 std::vector<const config*> candidate_traits;
836 std::vector<std::string> temp_require_traits;
837 std::vector<std::string> temp_exclude_traits;
841 int nb_traits = current_traits.size();
843 for(; nb_traits < max_traits; ++nb_traits)
846 candidate_traits.clear();
849 const std::string& tid =
t[
"id"];
850 bool already =
false;
851 for(
const config& mod : current_traits) {
852 if(mod[
"id"] == tid) {
867 for(
const config& mod : current_traits) {
868 if (mod[
"exclude_traits"] !=
"") {
870 temp_exclude_traits.push_back(
c);
876 bool trait_req_met =
true;
877 for(
const std::string&
s : temp_require_traits) {
878 bool has_trait =
false;
879 for(
const config& mod : current_traits) {
884 trait_req_met =
false;
893 bool trait_exc_met =
true;
895 for(
const std::string&
s : temp_exclude_traits) {
896 bool has_exclusionary_trait =
false;
897 for(
const config& mod : current_traits) {
899 has_exclusionary_trait =
true;
902 has_exclusionary_trait =
true;
904 if(has_exclusionary_trait) {
905 trait_exc_met =
false;
913 const std::string& avl =
t[
"availability"];
916 if(!must_have_only && (!
can_recruit() || avl ==
"any")) {
917 candidate_traits.push_back(&
t);
921 if(candidate_traits.empty()) {
927 candidate_traits.
erase(candidate_traits.begin() + num);
936 std::vector<std::string> res;
941 res.push_back(mod[
"id"]);
943 if(mod_type ==
"advancement"){
945 res.push_back(mod[
"id"]);
955 if(
type ==
"advancement"){
971 auto ss = stats_storage_resetter(*
this,
true);
994 if(!new_type.
usage().empty()) {
999 if(!new_type.
ellipse().empty()) {
1044 return std::make_shared<attack_type>(atk);
1086 events.add_child(
"event", unit_event);
1091 events.add_child(
"event", ability_event);
1097 for(
const auto [key, special] : specials.all_children_view()) {
1099 events.add_child(
"event", special_event);
1142 return leader_crown_path;
1152 const double unit_energy = max_hitpoints > 0
1153 ?
static_cast<double>(hitpoints) / max_hitpoints
1156 if(1.0 == unit_energy) {
1157 return {33, 225, 0};
1158 }
else if(unit_energy > 1.0) {
1159 return {100, 255, 100};
1160 }
else if(unit_energy >= 0.75) {
1161 return {170, 255, 0};
1162 }
else if(unit_energy >= 0.5) {
1163 return {255, 175, 0};
1164 }
else if(unit_energy >= 0.25) {
1165 return {255, 155, 0};
1194 return {255, 255, 255};
1195 }
else if(mid_advance) {
1196 return {150, 255, 255};
1197 }
else if(far_advance) {
1198 return {0, 205, 205};
1200 }
else if(has_amla) {
1202 return {225, 0, 255};
1203 }
else if(mid_advance) {
1204 return {169, 30, 255};
1205 }
else if(far_advance) {
1206 return {139, 0, 237};
1208 return {170, 0, 255};
1212 return {0, 160, 225};
1217 bool major_amla =
false;
1218 bool has_amla =
false;
1220 major_amla |= adv[
"major_amla"].to_bool();
1235 std::vector<std::string> result;
1238 result.push_back(adv_type->type_name());
1240 WRN_UT <<
"unknown unit in advances_to list of type "
1273 if(goal_dur.empty()) {
1275 return !mod_dur.empty() && mod_dur !=
"forever";
1278 return mod_dur == goal_dur;
1284 const unit_type* rebuild_from =
nullptr;
1286 for(
const auto& mod_name : ModificationTypes) {
1299 else if(rebuild_from ==
nullptr) {
1300 rebuild_from = &
type();
1308 if(rebuild_from !=
nullptr) {
1371 std::set<std::string> all_states =
states_;
1374 all_states.insert(state.first);
1379 if(all_states.count(
"undrainable") && all_states.count(
"unpoisonable") && all_states.count(
"unplagueable")) {
1380 all_states.insert(
"not_living");
1390 return get_state(known_boolean_state_id);
1394 if(state ==
"not_living") {
1427 if(
p.second == state) {
1435 {
"slowed", STATE_SLOWED},
1436 {
"poisoned", STATE_POISONED},
1437 {
"petrified", STATE_PETRIFIED},
1438 {
"uncovered", STATE_UNCOVERED},
1439 {
"not_moved", STATE_NOT_MOVED},
1440 {
"unhealable", STATE_UNHEALABLE},
1441 {
"guardian", STATE_GUARDIAN},
1442 {
"invulnerable", STATE_INVULNERABLE},
1450 set_state(known_boolean_state_id, value);
1455 if(state ==
"not_living") {
1471 if(cfg[
"id"] == ability) {
1484 if(
i->cfg[
"id"] == ability) {
1507 for(
const auto& a_ptr :
attacks_) {
1508 if(a_ptr->get_changed()) {
1519 auto write_subtag = [&](
const std::string& key,
const config& child)
1523 if(!child.empty()) {
1544 cfg.
add_child(
"special_note")[
"note"] = note;
1549 cfg[
"halo"] = *
halo_;
1576 cfg[
"side"] =
side_;
1589 cfg[
"role"] =
role_;
1592 for(
const std::string& state :
get_states()) {
1593 status_flags[state] =
true;
1598 write_subtag(
"status", status_flags);
1606 cfg[
"overlays"] =
"";
1608 cfg[
"name"] =
name_;
1613 cfg[
"canrecruit"] =
true;
1673 if(!advancement.empty()) {
1674 cfg.
add_child(
"advancement", advancement);
1702 return utils::holds_alternative<upkeep_loyal>(
upkeep_);
1709 overlays_.push_back(
"misc/loyal-icon.png");
1723 if(!defense_abilities.
empty()) {
1733 const std::string& apply_to = cfg[
"apply_to"];
1734 if(!apply_to.empty()) {
1735 if(damage_name != apply_to) {
1736 if(apply_to.find(
',') != std::string::npos &&
1737 apply_to.find(damage_name) != std::string::npos) {
1738 const std::vector<std::string>& vals =
utils::split(apply_to);
1739 if(std::find(vals.begin(),vals.end(),damage_name) == vals.end()) {
1762 if(!resistance_list.
empty()) {
1773 if(!(!cfg.
has_attribute(
"active_on") || (attacker && cfg[
"active_on"] ==
"offense") || (!attacker && cfg[
"active_on"] ==
"defense"))) {
1787 unit_ability_list damage_type_list = opp_weapon->get_specials_and_abilities(
"damage_type");
1788 if(damage_type_list.
empty()){
1791 std::string replacement_type = opp_weapon->select_damage_type(damage_type_list,
"replacement_type", resistance_list);
1792 std::string type_damage = replacement_type.empty() ? damage_name : replacement_type;
1794 for(
auto&
i : damage_type_list) {
1795 if((*
i.ability_cfg).has_attribute(
"alternative_type")){
1796 max_res = std::max(max_res ,
resistance_value(resistance_list, (*
i.ability_cfg)[
"alternative_type"].str()));
1806 std::map<std::string,std::string> temp;
1825 const std::string&
image = adv[
"image"];
1833 const std::string& tt = adv[
"description"];
1846 std::vector<std::pair<std::string, std::string>> temp;
1847 std::pair<std::string, std::string> icon;
1850 icon.first = adv[
"icon"].str();
1851 icon.second = adv[
"description"].str();
1853 for(
unsigned j = 0, j_count =
modification_count(
"advancement", adv[
"id"]); j < j_count; ++j) {
1854 temp.push_back(icon);
1863 std::vector<config> res;
1865 if(adv[
"strict_amla"].to_bool() && !
advances_to_.empty()) {
1868 if(
auto filter = adv.optional_child(
"filter")) {
1874 if(
modification_count(
"advancement", adv[
"id"]) >=
static_cast<unsigned>(adv[
"max_times"].to_int(1))) {
1878 std::vector<std::string> temp_require =
utils::split(adv[
"require_amla"]);
1879 std::vector<std::string> temp_exclude =
utils::split(adv[
"exclude_amla"]);
1881 if(temp_require.empty() && temp_exclude.empty()) {
1886 std::sort(temp_require.begin(), temp_require.end());
1887 std::sort(temp_exclude.begin(), temp_exclude.end());
1889 std::vector<std::string> uniq_require, uniq_exclude;
1891 std::unique_copy(temp_require.begin(), temp_require.end(), std::back_inserter(uniq_require));
1892 std::unique_copy(temp_exclude.begin(), temp_exclude.end(), std::back_inserter(uniq_exclude));
1894 bool exclusion_found =
false;
1895 for(
const std::string&
s : uniq_exclude) {
1896 int max_num = std::count(temp_exclude.begin(), temp_exclude.end(),
s);
1898 if(mod_num >= max_num) {
1899 exclusion_found =
true;
1904 if(exclusion_found) {
1908 bool requirements_done =
true;
1909 for(
const std::string&
s : uniq_require) {
1910 int required_num = std::count(temp_require.begin(), temp_require.end(),
s);
1912 if(required_num > mod_num) {
1913 requirements_done =
false;
1918 if(requirements_done) {
1946 std::size_t res = 0;
1948 if(item[
"id"] ==
id) {
1954 if(mod_type ==
"advancement") {
1962 "alignment",
"attack",
"defense",
"ellipse",
"experience",
"fearless",
1963 "halo",
"healthy",
"hitpoints",
"image_mod",
"jamming",
"jamming_costs",
"level",
1964 "loyal",
"max_attacks",
"max_experience",
"movement",
"movement_costs",
1965 "new_ability",
"new_advancement",
"new_animation",
"new_attack",
"overlay",
"profile",
1966 "recall_cost",
"remove_ability",
"remove_advancement",
"remove_attacks",
"resistance",
1967 "status",
"type",
"variation",
"vision",
"vision_costs",
"zoc"
1972 if(apply_to ==
"attack") {
1973 std::vector<t_string> attack_names;
1977 bool affected = a->describe_modification(effect, &desc);
1978 if(affected && !desc.empty()) {
1979 attack_names.emplace_back(a->name(),
"wesnoth-units");
1982 if(!attack_names.empty()) {
1985 symbols[
"effect_description"] = desc;
1986 return VGETTEXT(
"$attack_list|: $effect_description", symbols);
1988 }
else if(apply_to ==
"hitpoints") {
1989 const std::string& increase_total = effect[
"increase_total"];
1990 if(!increase_total.empty()) {
1992 "<span color=\"$color\">$number_or_percent</span> HP",
1993 {{
"number_or_percent",
utils::print_modifier(increase_total)}, {
"color", increase_total[0] ==
'-' ?
"#f00" :
"#0f0"}});
1996 const std::string& increase = effect[
"increase"];
1997 if(increase.empty()) {
2000 if(apply_to ==
"movement") {
2002 "<span color=\"$color\">$number_or_percent</span> move",
2003 "<span color=\"$color\">$number_or_percent</span> moves",
2004 std::stoi(increase),
2005 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2006 }
else if(apply_to ==
"vision") {
2008 "<span color=\"$color\">$number_or_percent</span> vision",
2009 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2010 }
else if(apply_to ==
"jamming") {
2012 "<span color=\"$color\">$number_or_percent</span> jamming",
2013 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2014 }
else if(apply_to ==
"max_experience") {
2017 "<span color=\"$color\">$number_or_percent</span> XP to advance",
2018 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
2019 }
else if(apply_to ==
"max_attacks") {
2021 "<span color=\"$color\">$number_or_percent</span> attack per turn",
2022 "<span color=\"$color\">$number_or_percent</span> attacks per turn",
2023 std::stoi(increase),
2024 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2025 }
else if(apply_to ==
"recall_cost") {
2028 "<span color=\"$color\">$number_or_percent</span> cost to recall",
2029 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
2039 if(apply_to ==
"fearless") {
2042 }
else if(apply_to ==
"healthy") {
2045 }
else if(apply_to ==
"profile") {
2059 for(
const config&
c : cfg_range) {
2060 if(!
c[
"remove"].to_bool()) {
2070 }
else if(apply_to ==
"new_attack") {
2074 for(
const auto [key, special] : specials.all_children_view()) {
2076 events.add_child(
"event", special_event);
2080 }
else if(apply_to ==
"remove_attacks") {
2083 }
else if(apply_to ==
"attack") {
2086 a->apply_modification(effect);
2088 for(
const auto [key, special] : specials.all_children_view()) {
2090 events.add_child(
"event", special_event);
2095 }
else if(apply_to ==
"hitpoints") {
2097 const std::string& increase_hp = effect[
"increase"];
2098 const std::string& increase_total = effect[
"increase_total"];
2099 const std::string& set_hp = effect[
"set"];
2100 const std::string& set_total = effect[
"set_total"];
2103 const bool violate_max = effect[
"violate_maximum"].to_bool();
2105 if(!set_hp.empty()) {
2106 if(set_hp.back() ==
'%') {
2113 if(!set_total.empty()) {
2114 if(set_total.back() ==
'%') {
2121 if(!increase_total.empty()) {
2129 if(effect[
"heal_full"].to_bool()) {
2133 if(!increase_hp.empty()) {
2139 LOG_UT <<
"resetting hp to max";
2146 }
else if(apply_to ==
"movement") {
2147 const bool apply_to_vision = effect[
"apply_to_vision"].to_bool(
true);
2156 const std::string& increase = effect[
"increase"];
2157 if(!increase.empty()) {
2167 if(apply_to_vision) {
2170 }
else if(apply_to ==
"vision") {
2176 const std::string& increase = effect[
"increase"];
2177 if(!increase.empty()) {
2182 }
else if(apply_to ==
"jamming") {
2183 const std::string& increase = effect[
"increase"];
2185 if(!increase.empty()) {
2190 }
else if(apply_to ==
"experience") {
2191 const std::string& increase = effect[
"increase"];
2192 const std::string&
set = effect[
"set"];
2195 if(
set.back() ==
'%') {
2202 if(increase.empty() ==
false) {
2205 }
else if(apply_to ==
"max_experience") {
2206 const std::string& increase = effect[
"increase"];
2207 const std::string&
set = effect[
"set"];
2209 if(
set.empty() ==
false) {
2210 if(
set.back() ==
'%') {
2217 if(increase.empty() ==
false) {
2222 }
else if(apply_to ==
"status") {
2223 const std::string& add = effect[
"add"];
2224 const std::string&
remove = effect[
"remove"];
2241 }
else if(apply_to ==
"zoc") {
2246 }
else if(apply_to ==
"new_ability") {
2250 for(
const auto [key, cfg] : ab_effect->all_children_view()) {
2254 events.add_child(
"event", event);
2260 }
else if(apply_to ==
"remove_ability") {
2262 for(
const auto [key, cfg] : ab_effect->all_children_view()) {
2269 if(
auto fab_effect = effect.
optional_child(
"experimental_filter_ability")) {
2273 }
else if(apply_to ==
"image_mod") {
2274 LOG_UT <<
"applying image_mod";
2275 std::string mod = effect[
"replace"];
2279 LOG_UT <<
"applying image_mod";
2280 mod = effect[
"add"].str();
2290 LOG_UT <<
"applying image_mod";
2291 }
else if(apply_to ==
"new_animation") {
2292 anim_comp_->apply_new_animation_effect(effect);
2293 }
else if(apply_to ==
"ellipse") {
2295 }
else if(apply_to ==
"halo") {
2297 }
else if(apply_to ==
"overlay") {
2298 const std::string& add = effect[
"add"];
2299 const std::string& replace = effect[
"replace"];
2300 const std::string&
remove = effect[
"remove"];
2312 if(add.empty() &&
remove.empty() && !replace.empty()) {
2315 }
else if(apply_to ==
"new_advancement") {
2316 const std::string& types = effect[
"types"];
2317 const bool replace = effect[
"replace"].to_bool(
false);
2320 if(!types.empty()) {
2325 std::copy(temp_advances.begin(), temp_advances.end(), std::back_inserter(
advances_to_));
2338 }
else if(apply_to ==
"remove_advancement") {
2339 const std::string& types = effect[
"types"];
2340 const std::string& amlas = effect[
"amlas"];
2345 for(
const std::string&
unit : temp_advances) {
2355 if(std::find(temp_advances.begin(), temp_advances.end(),
advancements_[
i][
"id"]) != temp_advances.end()) {
2359 }
else if(apply_to ==
"alignment") {
2364 }
else if(apply_to ==
"max_attacks") {
2365 const std::string& increase = effect[
"increase"];
2367 if(!increase.empty()) {
2370 }
else if(apply_to ==
"recall_cost") {
2371 const std::string& increase = effect[
"increase"];
2372 const std::string&
set = effect[
"set"];
2377 if(
set.back() ==
'%') {
2384 if(!increase.empty()) {
2387 }
else if(effect[
"apply_to"] ==
"variation") {
2389 assert(base_type !=
nullptr);
2390 const std::string& variation_id = effect[
"name"];
2394 if(effect[
"heal_full"].to_bool(
false)) {
2398 WRN_UT <<
"unknown variation '" << variation_id <<
"' (name=) in [effect]apply_to=variation, ignoring";
2400 }
else if(effect[
"apply_to"] ==
"type") {
2401 std::string prev_type = effect[
"prev_type"];
2402 if(prev_type.empty()) {
2405 const std::string& new_type_id = effect[
"name"];
2410 if(effect[
"heal_full"].to_bool(
false)) {
2414 WRN_UT <<
"unknown type '" << new_type_id <<
"' (name=) in [effect]apply_to=type, ignoring";
2416 }
else if(effect[
"apply_to"] ==
"level") {
2417 const std::string& increase = effect[
"increase"];
2418 const std::string&
set = effect[
"set"];
2425 level_ = lexical_cast_default<int>(
set);
2428 if(!increase.empty()) {
2429 level_ += lexical_cast_default<int>(increase);
2441 bool generate_description = mod[
"generate_description"].to_bool(
true);
2443 config* target =
nullptr;
2445 if(no_add ==
false) {
2450 std::vector<t_string> effects_description;
2457 if(
auto afilter = effect.optional_child(
"filter")) {
2463 const std::string& apply_to = effect[
"apply_to"];
2464 int times = effect[
"times"].to_int(1);
2467 if(no_add && (apply_to ==
"type" || apply_to ==
"variation")) {
2471 if(effect[
"times"] ==
"per level") {
2472 if(effect[
"apply_to"] ==
"level") {
2473 WRN_UT <<
"[effect] times=per level is not allowed with apply_to=level, using default value of 1";
2484 std::string description_component;
2495 description += description_component;
2506 if(effect[
"times"] ==
"per level" && !times) {
2507 description =
VGETTEXT(
"$effect_description per level", {{
"effect_description", description}});
2510 if(!description.
empty()) {
2511 effects_description.push_back(description);
2517 const t_string& mod_description = mod[
"description"];
2518 if(!mod_description.
empty()) {
2519 description = mod_description;
2524 if(generate_description && !effects_description.empty()) {
2525 if(!mod_description.
empty()) {
2526 description +=
"\n";
2529 for(
const auto& desc_line : effects_description) {
2530 description += desc_line +
"\n";
2535 if(mod_type ==
"trait") {
2547 const t_string name = gender_specific_name.
empty() ? trait[
"name"] : gender_specific_name;
2582 DBG_UT <<
"unit::invisible called: id = " <<
id() <<
" loc = " << loc <<
" get_loc = " <<
get_location();
2599 return itor->second;
2604 static const std::string hides(
"hides");
2613 units_with_cache.push_back(
this);
2674 std::stringstream ss;
2692 std::string::size_type pos =
id_.find_last_of(
'-');
2693 if(pos != std::string::npos && pos+1 <
id_.size()
2694 &&
id_.find_first_not_of(
"0123456789", pos+1) == std::string::npos) {
2696 WRN_UT <<
"assigning new id to clone of generic unit " <<
id_;
2706 : u_(const_cast<
unit&>(u))
2707 , moves_(u.movement_left(true))
2723 DBG_UT <<
"The unit to be removed is not in the unit map.";
2804 }
catch(std::invalid_argument&
e) {
2805 WRN_UT <<
"Found invalid upkeep=\"" <<
e.what() <<
"\" in a unit";
2818 for(
const auto& a_ptr :
attacks_) {
2819 a_ptr->set_changed(
false);
2833 u.
write(unit_config);
2835 static const std::set<std::string_view> main_keys {
2842 "ignore_race_traits",
2843 "ignore_global_traits",
2860 for(
const std::string_view& main_key : main_keys) {
2861 wcfg[main_key] = unit_config[main_key];
2864 static const std::set<std::string_view> attack_keys {
2875 for(
const std::string_view& attack_key : attack_keys) {
2876 child[attack_key] = att[attack_key];
2909 static const std::set<std::string_view> child_keys {
2918 for(
const std::string_view& child_key : child_keys) {
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands.
void append_active_ai_for_side(ai::side_number side, const config &cfg)
Appends AI parameters to active AI of the given side.
static manager & get_singleton()
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
void append(const config &cfg)
Append data from another config object to this one.
all_children_iterator erase(const all_children_iterator &i)
const_all_children_iterator ordered_begin() const
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.
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
void recursive_clear_value(config_key_type key)
void remove_child(config_key_type key, std::size_t index)
const_attr_itors attribute_range() const
std::size_t child_count(config_key_type key) const
auto all_children_view() const
In-order iteration over all children.
const_all_children_iterator ordered_end() const
void clear_children(T... keys)
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
bool has_attribute(config_key_type key) const
child_itors child_range(config_key_type key)
boost::iterator_range< const_attribute_iterator > const_attr_itors
std::size_t all_children_count() const
void remove_children(config_key_type key, std::function< bool(const config &)> p={})
Removes all children with tag key for which p returns true.
boost::iterator_range< const_child_iterator > const_child_itors
void append_children(const config &cfg)
Adds children from cfg.
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.
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
config & add_child(config_key_type key)
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 display * get_singleton()
Returns the display object if a display object exists.
n_unit::id_manager & unit_id_manager()
static game_config_view wrap(const config &cfg)
@ INITIAL
creating intitial [unit]s, executing toplevel [lua] etc.
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type=std::string())
std::string apply_effect(const std::string &name, unit &u, const config &cfg, bool need_apply)
void write(config &cfg, bool include_notes) const
Writes the movement type data to the provided config.
static const std::set< std::string > effects
The set of applicable effects for movement types.
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...
int defense_modifier(const t_translation::terrain_code &terrain) const
Returns the defensive value of the indicated terrain.
int resistance_against(const std::string &damage_type) const
Returns the vulnerability to the indicated damage type (higher means takes more damage).
static id_manager & global_instance()
unit_id next_id()
returns id for unit that is created
std::set< std::string > & encountered_units()
static rng & default_instance()
int get_random_int(int min, int max)
This class stores all the data for a single 'side' (in game nomenclature).
bool is_enemy(int n) const
static std::string get_side_color_id(unsigned side)
bool fogged(const map_location &loc) const
Visitor helper class to parse the upkeep value from a config.
Visitor helper class to fetch the appropriate upkeep value.
int get_composite_value() const
const std::string & id() const
static const unit_race null_race
Dummy race used when a race is not yet known.
std::string generate_name(GENDER gender) const
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.
const unit_race * find_race(const std::string &) const
void check_types(const std::vector< std::string > &types) const
A single unit type that the player may recruit.
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...
const std::string & parent_id() const
The id of the original type from which this (variation) descended.
const std::string & image() const
config::const_child_itors advancements() const
const std::string & variation_id() const
The id of this variation; empty if it's a gender variation or a base unit.
const std::string & id() const
The id for this unit_type.
const movetype & movement_type() const
const unit_race * race() const
Never returns nullptr, but may point to the null race.
double xp_bar_scaling() const
const std::string & default_variation() const
const unit_type & get_variation(const std::string &id) const
const_attack_itors attacks() const
const std::string & usage() const
const std::vector< std::string > & advances_to() const
A vector of unit_type ids that this unit_type can advance to.
bool has_variation(const std::string &variation_id) const
std::string ellipse() const
t_string unit_description() const
static void check_id(std::string &id)
Validate the id argument.
const std::string & flag_rgb() const
const config & abilities_cfg() const
const std::string log_id() const
A variant on id() that is more descriptive, for use with message logging.
const unit_type & get_gender_unit_type(std::string gender) const
Returns a gendered variant of this unit_type.
const std::string & icon() const
int experience_needed(bool with_acceleration=true) const
bool generate_name() const
const std::string & big_profile() const
const std::string & undead_variation() const
Info on the type of unit that the unit reanimates as.
double hp_bar_scaling() const
const t_string & type_name() const
The name of the unit in the current language setting.
config::const_child_itors possible_traits() const
unit_alignments::type alignment() const
const std::string & small_profile() const
const config & get_cfg() const
unsigned int num_traits() const
This class represents a single unit of a specific type.
static void clear_status_caches()
Clear this unit status cache for all units.
void set_attr_changed(UNIT_ATTRIBUTE attr)
bool get_attr_changed(UNIT_ATTRIBUTE attr) const
void clear_changed_attributes()
void init(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
static const std::string & leader_crown()
The path to the leader crown overlay.
bool get_attacks_changed() const
A variable-expanding proxy for the config class.
std::vector< vconfig > child_list
vconfig child(const std::string &key) const
Returns a child of *this whose key is key.
const config & get_config() const
child_list get_children(const std::string &key) const
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
map_display and display: classes which take care of displaying the map and game-data on the screen.
Interfaces for manipulating version numbers of engine, add-ons, etc.
void generate_name()
Generates a random race-appropriate name if one has not already been provided.
std::vector< t_string > trait_names_
~unit_movement_resetter()
void remove_ability_by_attribute(const config &filter)
Removes a unit's abilities with a specific ID or other attribute.
std::string undead_variation_
t_string type_name_
The displayed name of this unit type.
unit_movement_resetter(const unit_movement_resetter &)=delete
void write(config &cfg, bool write_all=true) const
Serializes the current unit metadata values.
std::bitset< UA_COUNT > changed_attributes_
std::string small_profile_
void write_upkeep(config::attribute_value &upkeep) const
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.
std::vector< t_string > special_notes_
static std::map< std::string, state_t > known_boolean_state_names_
utils::optional< std::string > ellipse_
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
const unit_type * type_
Never nullptr.
std::bitset< num_bool_states > known_boolean_states_
utils::optional< std::string > halo_
map_location::direction facing_
const unit_race * race_
Never nullptr, but may point to the null race.
unit_alignments::type alignment_
bool invisible(const map_location &loc, bool see_all=true) const
bool is_visible_to_team(const team &team, bool const see_all=true) const
n_unit::unit_id underlying_id_
unit & mark_clone(bool is_temporary)
Mark this unit as clone so it can be inserted to unit_map.
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.
unit_race::GENDER gender_
std::map< map_location, bool > invisibility_cache_
Hold the visibility status cache for a unit, when not uncovered.
std::vector< std::string > advances_to_
std::unique_ptr< unit_animation_component > anim_comp_
static std::string type()
void remove_ability_by_id(const std::string &ability)
Removes a unit's abilities with a specific ID.
utils::optional< std::string > usage_
std::vector< std::string > overlays_
bool has_ability_by_id(const std::string &ability) const
Check if the unit has an ability of a specific ID.
const config & abilities() const
void parse_upkeep(const config::attribute_value &upkeep)
std::vector< std::string > recruit_list_
std::vector< config > advancements_
utils::string_map modification_descriptions_
unit_checksum_version
Optional parameter for get_checksum to use the algorithm of an older version of Wesnoth,...
std::vector< std::string > trait_nonhidden_ids_
std::set< std::string > states_
bool ability_matches_filter(const config &cfg, const std::string &tag_name, const config &filter) const
Verify what abilities attributes match with filter.
std::unique_ptr< unit_formula_manager > formula_man_
std::vector< t_string > trait_descriptions_
@ version_1_16_or_older
Included some of the flavortext from weapon specials.
void set_big_profile(const std::string &value)
int max_hitpoints() const
The max number of hitpoints this unit can have.
void heal(int amount)
Heal the unit.
void set_state(const std::string &state, bool value)
Set whether the unit is affected by a status effect.
void new_turn()
Refresh unit for the beginning of a turn.
const std::vector< std::string > & recruits() const
The type IDs of the other units this unit may recruit, if possible.
void set_max_experience(int value)
void set_max_hitpoints(int value)
void set_hitpoints(int hp)
Sets the current hitpoint amount.
int recall_cost() const
How much gold it costs to recall this unit, or -1 if the side's default recall cost is used.
std::string big_profile() const
An optional profile image displays when this unit is 'speaking' via [message].
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.
void set_level(int level)
Sets the current level of this unit.
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
const std::string & variation() const
The ID of the variation of this unit's type.
int hitpoints() const
The current number of hitpoints this unit has.
static std::string get_known_boolean_state_name(state_t state)
Convert a built-in status effect ID to a string status effect ID.
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
std::string small_profile() const
An optional profile image to display in Help.
void heal_fully()
Fully heal the unit, restoring it to max hitpoints.
void set_undead_variation(const std::string &value)
The ID of the undead variation (ie, dwarf, swimmer) of this unit.
const std::string & type_id() const
The id of this unit's type.
void set_alignment(unit_alignments::type alignment)
Sets the alignment of this unit.
const std::set< std::string > get_states() const
Get the status effects currently affecting the unit.
void new_scenario()
Refresh unit for the beginning of a new scenario.
void end_turn()
Refresh unit for the end of a turn.
const unit_type & type() const
This unit's type, accounting for gender and variation.
int experience() const
The current number of experience points this unit has.
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
void set_experience(int xp)
Sets the current experience point amount.
const std::string & id() const
Gets this unit's id.
void set_underlying_id(n_unit::id_manager &id_manager)
Sets the internal ID.
int side() const
The side this unit belongs to.
unsigned int experience_to_advance() const
The number of experience points this unit needs to level up, or 0 if current XP > max XP.
state_t
Built-in status effects known to the engine.
void set_recruits(const std::vector< std::string > &recruits)
Sets the recruit list.
std::vector< t_string > unit_special_notes() const
The unit's special notes.
config & variables()
Gets any user-defined variables this unit 'owns'.
void set_usage(const std::string &usage)
Sets this unit's usage.
void set_small_profile(const std::string &value)
unit_race::GENDER gender() const
The gender of this unit.
const t_string & name() const
Gets this unit's translatable display name.
@ STATE_UNKNOWN
To set the size of known_boolean_states_.
@ STATE_NOT_MOVED
The unit is uncovered - it was hiding but has been spotted.
@ STATE_GUARDIAN
The unit cannot be healed.
@ STATE_INVULNERABLE
The unit is a guardian - it won't move unless a target is sighted.
@ STATE_PETRIFIED
The unit is poisoned - it loses health each turn.
@ STATE_POISONED
The unit is slowed - it moves slower and does less damage.
@ STATE_UNCOVERED
The unit is petrified - it cannot move or be attacked.
std::vector< std::string > advances_to_t
std::vector< config > get_modification_advances() const
Gets any non-typed advanced options set by modifications.
std::vector< std::pair< std::string, std::string > > amla_icons() const
Gets the image and description data for modification advancements.
const advances_to_t & advances_to() const
Gets the possible types this unit can advance to on level-up.
bool can_advance() const
Checks whether this unit has any options to advance to.
void set_advancements(std::vector< config > advancements)
Sets the raw modification advancement option data.
void set_advances_to(const std::vector< std::string > &advances_to)
Sets this unit's advancement options.
const std::vector< config > & modification_advancements() const
The raw, unparsed data for modification advancements.
std::map< std::string, std::string > advancement_icons() const
Gets and image path and and associated description for each advancement option.
const std::vector< std::string > advances_to_translated() const
Gets the names of the possible types this unit can advance to on level-up.
void advance_to(const unit_type &t, bool use_traits=false)
Advances this unit to another type.
void remove_attacks_ai()
Set the unit to have no attacks left for this turn.
void set_max_attacks(int value)
int defense_modifier(const t_translation::terrain_code &terrain) const
The unit's defense on a given terrain.
bool resistance_filter_matches(const config &cfg, const std::string &damage_name, int res) const
bool remove_attack(attack_ptr atk)
Remove an attack from the unit.
attack_itors attacks()
Gets an iterator over this unit's attacks.
int max_attacks() const
The maximum number of attacks this unit may perform per turn, usually 1.
int resistance_value(unit_ability_list resistance_list, const std::string &damage_name) const
For the provided list of resistance abilities, determine the damage resistance based on which are act...
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.
int attacks_left() const
Gets the remaining number of attacks this unit can perform this turn.
void set_attacks(int left)
Sets the number of attacks this unit has left this turn.
color_t xp_color() const
Color for this unit's XP.
color_t hp_color() const
Color for this unit's current hitpoints.
std::string TC_image_mods() const
Constructs a recolor (RC) IPF string for this unit's team color.
static color_t hp_color_max()
const std::string & flag_rgb() const
Get the source color palette to use when recoloring the unit's image.
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
std::string default_anim_image() const
The default image to use for animation frames with no defined image.
const std::vector< std::string > & overlays() const
Get the unit's overlay images.
std::string absolute_image() const
The name of the file to game_display (used in menus).
void set_image_ellipse(const std::string &ellipse)
Set the unit's ellipse image.
void set_image_halo(const std::string &halo)
Set the unit's halo image.
void apply_builtin_effect(std::string type, const config &effect)
Apply a builtin effect to the unit.
void add_modification(const std::string &type, const config &modification, bool no_add=false)
Add a new modification to the unit.
static const std::set< std::string > builtin_effects
std::string describe_builtin_effect(std::string type, const config &effect)
Construct a string describing a built-in effect.
void apply_modifications()
Re-apply all saved modifications.
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.
const map_location & get_location() const
The current map location this unit is at.
const movetype & movement_type() const
Get the unit's movement type.
void set_movement(int moves, bool unit_action=false)
Set this unit's remaining movement to moves.
void set_facing(map_location::direction dir) const
The this unit's facing.
void set_total_movement(int value)
void set_emit_zoc(bool val)
Sets the raw zone-of-control flag.
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
int total_movement() const
The maximum moves this unit has.
void remove_movement_ai()
Sets the unit to have no moves left for this turn.
void set_interrupted_move(const map_location &interrupted_move)
Set the target location of the unit's interrupted move.
std::vector< std::string > get_modifications_list(const std::string &mod_type) const
Gets a list of the modification this unit currently has.
int upkeep() const
Gets the amount of gold this unit costs a side per turn.
void add_trait_description(const config &trait, const t_string &description)
Register a trait's name and its description for the UI's use.
void generate_traits(bool must_have_only=false)
Applies mandatory traits (e.g.
void set_loyal(bool loyal)
bool loyal() const
Gets whether this unit is loyal - ie, it costs no upkeep.
std::string tooltip
Shown when hovering over an entry in the filter's drop-down list.
New lexcical_cast header.
Standard logging facilities (interface).
#define log_scope(description)
A small explanation about what's going on here: Each action has access to two game_info objects First...
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Handling of system events.
static void add_color_info(const game_config_view &v, bool build_defaults)
void remove()
Removes a tip.
Functions to load and save images from/to disk.
rng * generator
This generator is automatically synced during synced context.
game_events::manager * game_events
game_lua_kernel * lua_kernel
filter_context * filter_con
bool filter_base_matches(const config &cfg, int def)
std::size_t erase(Container &container, const Value &value)
Convenience wrapper for using std::remove on a container.
std::string get_unknown_exception_type()
Utility function for finding the type of thing caught with catch(...).
std::vector< std::string > parenthetical_split(std::string_view val, const char separator, std::string_view left, std::string_view right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
int apply_modifier(const int number, const std::string &amount, const int minimum)
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::string format_conjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a conjunctive list.
std::map< std::string, t_string > string_map
std::vector< std::string > split(const config_attribute_value &val)
std::string print_modifier(const std::string &mod)
Add a "+" or replace the "-" par Unicode minus.
std::string::const_iterator iterator
std::shared_ptr< const attack_type > const_attack_ptr
std::shared_ptr< attack_type > attack_ptr
const std::string & gender_string(unit_race::GENDER gender)
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
const config::attribute_value & gender_value(const config &cfg, unit_race::GENDER gender, const std::string &male_key, const std::string &female_key, const std::string &default_key)
Chooses a value from the given config based on gender.
The basic class for representing 8-bit RGB or RGBA colour values.
Encapsulates the map of the game.
static std::string write_direction(direction dir)
direction
Valid directions which can be moved in our hexagonal world.
static direction parse_direction(const std::string &str)
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.
static constexpr utils::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Visitor helper struct to fetch the upkeep type flag if applicable, or the the value otherwise.
Data typedef for unit_ability_list.
void validate_side(int side)
static map_location::direction s
unit_type_data unit_types
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().
void adjust_profile(std::string &profile)
static lg::log_domain log_unit("unit")
static const unit_type & get_unit_type(const std::string &type_id)
Converts a string ID to a unit_type.
static unit_race::GENDER generate_gender(const unit_type &type, bool random_gender)
bool mod_duration_match(const std::string &mod_dur, const std::string &goal_dur)
Determines if mod_dur "matches" goal_dur.
static bool resistance_filter_matches_base(const config &cfg, bool attacker)
std::string get_checksum(const unit &u, backwards_compatibility::unit_checksum_version version)
Gets a checksum for a unit.
static color_t hp_color_impl(int hitpoints, int max_hitpoints)