57 #include <string_view>
63 #define DBG_UT LOG_STREAM(debug, log_unit)
64 #define LOG_UT LOG_STREAM(info, log_unit)
65 #define WRN_UT LOG_STREAM(warn, log_unit)
66 #define ERR_UT LOG_STREAM(err, log_unit)
71 const std::set<std::string_view> ModificationTypes {
"advancement",
"advance",
"trait",
"object" };
78 static std::vector<const unit*> units_with_cache;
80 static const std::string leader_crown_path =
"misc/leader-crown.png";
81 static const std::set<std::string_view> internalized_attrs {
152 auto cur_known = internalized_attrs.begin();
153 auto end_known = internalized_attrs.end();
155 while(cur_known != end_known) {
159 int comp = cur->first.compare(*cur_known);
161 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
174 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
179 auto stats_storage_resetter(
unit& u,
bool clamp =
false)
216 if(type_id.empty()) {
219 std::string new_id = type_id;
228 const std::vector<unit_race::GENDER>& genders =
type.genders();
229 assert(genders.size() > 0);
231 if(random_gender ==
false || genders.size() == 1) {
232 return genders.front();
240 const std::string& gender = cfg[
"gender"];
241 if(!gender.empty()) {
250 : std::enable_shared_from_this<
unit>()
252 , advances_to_(o.advances_to_)
254 , type_name_(o.type_name_)
258 , underlying_id_(o.underlying_id_)
259 , undead_variation_(o.undead_variation_)
260 , variation_(o.variation_)
261 , hit_points_(o.hit_points_)
262 , max_hit_points_(o.max_hit_points_)
263 , experience_(o.experience_)
264 , max_experience_(o.max_experience_)
266 , recall_cost_(o.recall_cost_)
267 , canrecruit_(o.canrecruit_)
268 , recruit_list_(o.recruit_list_)
269 , alignment_(o.alignment_)
270 , flag_rgb_(o.flag_rgb_)
271 , image_mods_(o.image_mods_)
272 , unrenamable_(o.unrenamable_)
276 , movement_(o.movement_)
277 , max_movement_(o.max_movement_)
279 , jamming_(o.jamming_)
280 , movement_type_(o.movement_type_)
281 , hold_position_(o.hold_position_)
282 , end_turn_(o.end_turn_)
283 , resting_(o.resting_)
284 , attacks_left_(o.attacks_left_)
285 , max_attacks_(o.max_attacks_)
287 , known_boolean_states_(o.known_boolean_states_)
288 , variables_(o.variables_)
290 , filter_recall_(o.filter_recall_)
291 , emit_zoc_(o.emit_zoc_)
292 , overlays_(o.overlays_)
294 , attacks_(o.attacks_)
296 , trait_names_(o.trait_names_)
297 , trait_descriptions_(o.trait_descriptions_)
298 , trait_nonhidden_ids_(o.trait_nonhidden_ids_)
299 , unit_value_(o.unit_value_)
301 , interrupted_move_(o.interrupted_move_)
302 , is_fearless_(o.is_fearless_)
303 , is_healthy_(o.is_healthy_)
304 , modification_descriptions_(o.modification_descriptions_)
307 , hp_bar_scaling_(o.hp_bar_scaling_)
308 , xp_bar_scaling_(o.xp_bar_scaling_)
309 , modifications_(o.modifications_)
310 , abilities_(o.abilities_)
311 , advancements_(o.advancements_)
312 , description_(o.description_)
313 , special_notes_(o.special_notes_)
316 , ellipse_(o.ellipse_)
317 , random_traits_(o.random_traits_)
318 , generate_name_(o.generate_name_)
320 , profile_(o.profile_)
321 , small_profile_(o.small_profile_)
322 , changed_attributes_(o.changed_attributes_)
323 , invisibility_cache_()
332 : std::enable_shared_from_this<
unit>()
341 , undead_variation_()
354 , unrenamable_(false)
363 , hold_position_(false)
369 , known_boolean_states_()
379 , trait_descriptions_()
380 , trait_nonhidden_ids_()
383 , interrupted_move_()
384 , is_fearless_(false)
386 , modification_descriptions_()
399 , random_traits_(true)
400 , generate_name_(true)
402 , changed_attributes_(0)
403 , invisibility_cache_()
412 id_ = cfg[
"id"].str();
417 role_ = cfg[
"role"].str();
420 hidden_ = cfg[
"hidden"].to_bool(
false);
425 side_ = cfg[
"side"].to_int();
436 const vconfig& filter_recall = vcfg->
child(
"filter_recall");
437 if(!filter_recall.
null())
445 for(
const vconfig& abilities_tag : abilities_tags) {
446 for(
const auto& [key, child] : abilities_tag.all_ordered()) {
448 for(
const vconfig& ability_event : ability_events) {
456 for(
const vconfig& specials_tag : specials_tags) {
457 for(
const auto& [key, child] : specials_tag.all_ordered()) {
459 for(
const vconfig& special_event : special_events) {
480 for(
const auto [key, special] : specials.all_children_view()) {
509 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.");
512 effect[
"apply_to"] =
"overlay";
513 effect[
"add"] = v->str();
589 if(temp_advances.size() == 1 && temp_advances.front() ==
"null") {
591 }
else if(temp_advances.size() >= 1 && !temp_advances.front().empty()) {
598 for(
config mai :
ai->child_range(
"micro_ai")) {
600 mai.add_child(
"filter")[
"id"] =
id();
601 mai[
"side"] =
side();
602 mai[
"action"] =
"add";
605 for(
config ca :
ai->child_range(
"candidate_action")) {
607 ca.add_child(
"filter_own")[
"id"] =
id();
609 ca.remove_attribute(
"sticky");
610 std::string stage =
"main_loop";
611 if(ca.has_attribute(
"stage")) {
612 stage = ca[
"stage"].str();
613 ca.remove_attribute(
"stage");
618 "path",
"stage[" + stage +
"].candidate_action[]",
619 "candidate_action", ca,
632 for(
const config&
c : cfg_range) {
641 for(
const config&
c : cfg_range) {
688 for(
const auto& [key, value] : status_flags->attribute_range()) {
689 if(value.to_bool()) {
695 if(cfg[
"ai_special"] ==
"guardian") {
716 resting_ = cfg[
"resting"].to_bool();
721 if(!cfg[
"recall_cost"].
blank()) {
735 for(
const auto& anim :
anim_comp_->animations_) {
736 std::cout << anim.debug() << std::endl;
743 for(
auto& u : units_with_cache) {
744 u->clear_visibility_cache();
747 units_with_cache.clear();
757 facing_ = get_random_direction();
781 auto itor =
std::find(units_with_cache.begin(), units_with_cache.end(),
this);
783 if(itor != units_with_cache.end()) {
784 units_with_cache.erase(itor);
786 }
catch(
const std::exception &
e) {
787 ERR_UT <<
"Caught exception when destroying unit: " <<
e.what();
804 LOG_UT <<
"Generating a trait for unit type " <<
type().
log_id() <<
" with must_have_only " << must_have_only;
812 const std::string& tid =
t[
"id"];
813 bool already =
false;
814 for(
const config& mod : current_traits) {
815 if(mod[
"id"] == tid) {
824 const std::string& avl =
t[
"availability"];
825 if(avl ==
"musthave") {
836 std::vector<const config*> candidate_traits;
837 std::vector<std::string> temp_require_traits;
838 std::vector<std::string> temp_exclude_traits;
842 int nb_traits = current_traits.size();
844 for(; nb_traits < max_traits; ++nb_traits)
847 candidate_traits.clear();
850 const std::string& tid =
t[
"id"];
851 bool already =
false;
852 for(
const config& mod : current_traits) {
853 if(mod[
"id"] == tid) {
868 for(
const config& mod : current_traits) {
869 if (mod[
"exclude_traits"] !=
"") {
871 temp_exclude_traits.push_back(
c);
877 bool trait_req_met =
true;
878 for(
const std::string&
s : temp_require_traits) {
879 bool has_trait =
false;
880 for(
const config& mod : current_traits) {
885 trait_req_met =
false;
894 bool trait_exc_met =
true;
896 for(
const std::string&
s : temp_exclude_traits) {
897 bool has_exclusionary_trait =
false;
898 for(
const config& mod : current_traits) {
900 has_exclusionary_trait =
true;
903 has_exclusionary_trait =
true;
905 if(has_exclusionary_trait) {
906 trait_exc_met =
false;
914 const std::string& avl =
t[
"availability"];
917 if(!must_have_only && (!
can_recruit() || avl ==
"any")) {
918 candidate_traits.push_back(&
t);
922 if(candidate_traits.empty()) {
928 candidate_traits.
erase(candidate_traits.begin() + num);
937 std::vector<std::string> res;
942 res.push_back(mod[
"id"]);
944 if(mod_type ==
"advancement"){
946 res.push_back(mod[
"id"]);
956 if(
type ==
"advancement"){
972 auto ss = stats_storage_resetter(*
this,
true);
995 if(!new_type.
usage().empty()) {
1000 if(!new_type.
ellipse().empty()) {
1045 return std::make_shared<attack_type>(atk);
1087 events.add_child(
"event", unit_event);
1092 events.add_child(
"event", ability_event);
1098 for(
const auto [key, special] : specials.all_children_view()) {
1100 events.add_child(
"event", special_event);
1143 return leader_crown_path;
1153 const double unit_energy = max_hitpoints > 0
1154 ?
static_cast<double>(hitpoints) / max_hitpoints
1157 if(1.0 == unit_energy) {
1158 return {33, 225, 0};
1159 }
else if(unit_energy > 1.0) {
1160 return {100, 255, 100};
1161 }
else if(unit_energy >= 0.75) {
1162 return {170, 255, 0};
1163 }
else if(unit_energy >= 0.5) {
1164 return {255, 175, 0};
1165 }
else if(unit_energy >= 0.25) {
1166 return {255, 155, 0};
1195 return {255, 255, 255};
1196 }
else if(mid_advance) {
1197 return {150, 255, 255};
1198 }
else if(far_advance) {
1199 return {0, 205, 205};
1201 }
else if(has_amla) {
1203 return {225, 0, 255};
1204 }
else if(mid_advance) {
1205 return {169, 30, 255};
1206 }
else if(far_advance) {
1207 return {139, 0, 237};
1209 return {170, 0, 255};
1213 return {0, 160, 225};
1218 bool major_amla =
false;
1219 bool has_amla =
false;
1221 major_amla |= adv[
"major_amla"].to_bool();
1236 std::vector<std::string> result;
1239 result.push_back(adv_type->type_name());
1241 WRN_UT <<
"unknown unit in advances_to list of type "
1274 if(goal_dur.empty()) {
1276 return !mod_dur.empty() && mod_dur !=
"forever";
1279 return mod_dur == goal_dur;
1285 const unit_type* rebuild_from =
nullptr;
1287 for(
const auto& mod_name : ModificationTypes) {
1300 else if(rebuild_from ==
nullptr) {
1301 rebuild_from = &
type();
1309 if(rebuild_from !=
nullptr) {
1372 std::set<std::string> all_states =
states_;
1375 all_states.insert(state.first);
1380 if(all_states.count(
"undrainable") && all_states.count(
"unpoisonable") && all_states.count(
"unplagueable")) {
1381 all_states.insert(
"not_living");
1391 return get_state(known_boolean_state_id);
1395 if(state ==
"not_living") {
1428 if(
p.second == state) {
1436 {
"slowed", STATE_SLOWED},
1437 {
"poisoned", STATE_POISONED},
1438 {
"petrified", STATE_PETRIFIED},
1439 {
"uncovered", STATE_UNCOVERED},
1440 {
"not_moved", STATE_NOT_MOVED},
1441 {
"unhealable", STATE_UNHEALABLE},
1442 {
"guardian", STATE_GUARDIAN},
1443 {
"invulnerable", STATE_INVULNERABLE},
1451 set_state(known_boolean_state_id, value);
1456 if(state ==
"not_living") {
1472 if(cfg[
"id"] == ability) {
1485 if(
i->cfg[
"id"] == ability) {
1508 for(
const auto& a_ptr :
attacks_) {
1509 if(a_ptr->get_changed()) {
1520 auto write_subtag = [&](
const std::string& key,
const config& child)
1524 if(!child.empty()) {
1545 cfg.
add_child(
"special_note")[
"note"] = note;
1550 cfg[
"halo"] = *
halo_;
1577 cfg[
"side"] =
side_;
1590 cfg[
"role"] =
role_;
1593 for(
const std::string& state :
get_states()) {
1594 status_flags[state] =
true;
1599 write_subtag(
"status", status_flags);
1607 cfg[
"overlays"] =
"";
1609 cfg[
"name"] =
name_;
1614 cfg[
"canrecruit"] =
true;
1674 if(!advancement.empty()) {
1675 cfg.
add_child(
"advancement", advancement);
1703 return utils::holds_alternative<upkeep_loyal>(
upkeep_);
1710 overlays_.push_back(
"misc/loyal-icon.png");
1724 if(!defense_abilities.
empty()) {
1734 const std::string& apply_to = cfg[
"apply_to"];
1735 if(!apply_to.empty()) {
1736 if(damage_name != apply_to) {
1737 if(apply_to.find(
',') != std::string::npos &&
1738 apply_to.find(damage_name) != std::string::npos) {
1739 const std::vector<std::string>& vals =
utils::split(apply_to);
1740 if(
std::find(vals.begin(),vals.end(),damage_name) == vals.end()) {
1763 if(!resistance_list.
empty()) {
1774 if(!(!cfg.
has_attribute(
"active_on") || (attacker && cfg[
"active_on"] ==
"offense") || (!attacker && cfg[
"active_on"] ==
"defense"))) {
1788 unit_ability_list damage_type_list = opp_weapon->get_specials_and_abilities(
"damage_type");
1789 if(damage_type_list.
empty()){
1792 std::string replacement_type = opp_weapon->select_damage_type(damage_type_list,
"replacement_type", resistance_list);
1793 std::string type_damage = replacement_type.empty() ? damage_name : replacement_type;
1795 for(
auto&
i : damage_type_list) {
1796 if((*
i.ability_cfg).has_attribute(
"alternative_type")){
1797 max_res = std::max(max_res ,
resistance_value(resistance_list, (*
i.ability_cfg)[
"alternative_type"].str()));
1807 std::map<std::string,std::string> temp;
1826 const std::string&
image = adv[
"image"];
1834 const std::string& tt = adv[
"description"];
1847 std::vector<std::pair<std::string, std::string>> temp;
1848 std::pair<std::string, std::string> icon;
1851 icon.first = adv[
"icon"].str();
1852 icon.second = adv[
"description"].str();
1854 for(
unsigned j = 0, j_count =
modification_count(
"advancement", adv[
"id"]); j < j_count; ++j) {
1855 temp.push_back(icon);
1864 std::vector<config> res;
1866 if(adv[
"strict_amla"].to_bool() && !
advances_to_.empty()) {
1869 if(
auto filter = adv.optional_child(
"filter")) {
1875 if(
modification_count(
"advancement", adv[
"id"]) >=
static_cast<unsigned>(adv[
"max_times"].to_int(1))) {
1879 std::vector<std::string> temp_require =
utils::split(adv[
"require_amla"]);
1880 std::vector<std::string> temp_exclude =
utils::split(adv[
"exclude_amla"]);
1882 if(temp_require.empty() && temp_exclude.empty()) {
1887 std::sort(temp_require.begin(), temp_require.end());
1888 std::sort(temp_exclude.begin(), temp_exclude.end());
1890 std::vector<std::string> uniq_require, uniq_exclude;
1892 std::unique_copy(temp_require.begin(), temp_require.end(), std::back_inserter(uniq_require));
1893 std::unique_copy(temp_exclude.begin(), temp_exclude.end(), std::back_inserter(uniq_exclude));
1895 bool exclusion_found =
false;
1896 for(
const std::string&
s : uniq_exclude) {
1897 int max_num = std::count(temp_exclude.begin(), temp_exclude.end(),
s);
1899 if(mod_num >= max_num) {
1900 exclusion_found =
true;
1905 if(exclusion_found) {
1909 bool requirements_done =
true;
1910 for(
const std::string&
s : uniq_require) {
1911 int required_num = std::count(temp_require.begin(), temp_require.end(),
s);
1913 if(required_num > mod_num) {
1914 requirements_done =
false;
1919 if(requirements_done) {
1947 std::size_t res = 0;
1949 if(item[
"id"] ==
id) {
1955 if(mod_type ==
"advancement") {
1963 "alignment",
"attack",
"defense",
"ellipse",
"experience",
"fearless",
1964 "halo",
"healthy",
"hitpoints",
"image_mod",
"jamming",
"jamming_costs",
"level",
1965 "loyal",
"max_attacks",
"max_experience",
"movement",
"movement_costs",
1966 "new_ability",
"new_advancement",
"new_animation",
"new_attack",
"overlay",
"profile",
1967 "recall_cost",
"remove_ability",
"remove_advancement",
"remove_attacks",
"resistance",
1968 "status",
"type",
"variation",
"vision",
"vision_costs",
"zoc"
1973 if(apply_to ==
"attack") {
1974 std::vector<t_string> attack_names;
1978 bool affected = a->describe_modification(effect, &desc);
1979 if(affected && !desc.empty()) {
1980 attack_names.emplace_back(a->name(),
"wesnoth-units");
1983 if(!attack_names.empty()) {
1986 symbols[
"effect_description"] = desc;
1987 return VGETTEXT(
"$attack_list|: $effect_description", symbols);
1989 }
else if(apply_to ==
"hitpoints") {
1990 const std::string& increase_total = effect[
"increase_total"];
1991 if(!increase_total.empty()) {
1993 "<span color=\"$color\">$number_or_percent</span> HP",
1994 {{
"number_or_percent",
utils::print_modifier(increase_total)}, {
"color", increase_total[0] ==
'-' ?
"#f00" :
"#0f0"}});
1997 const std::string& increase = effect[
"increase"];
1998 if(increase.empty()) {
2001 if(apply_to ==
"movement") {
2003 "<span color=\"$color\">$number_or_percent</span> move",
2004 "<span color=\"$color\">$number_or_percent</span> moves",
2006 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2007 }
else if(apply_to ==
"vision") {
2009 "<span color=\"$color\">$number_or_percent</span> vision",
2010 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2011 }
else if(apply_to ==
"jamming") {
2013 "<span color=\"$color\">$number_or_percent</span> jamming",
2014 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2015 }
else if(apply_to ==
"max_experience") {
2018 "<span color=\"$color\">$number_or_percent</span> XP to advance",
2019 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
2020 }
else if(apply_to ==
"max_attacks") {
2022 "<span color=\"$color\">$number_or_percent</span> attack per turn",
2023 "<span color=\"$color\">$number_or_percent</span> attacks per turn",
2025 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2026 }
else if(apply_to ==
"recall_cost") {
2029 "<span color=\"$color\">$number_or_percent</span> cost to recall",
2030 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
2040 if(apply_to ==
"fearless") {
2043 }
else if(apply_to ==
"healthy") {
2046 }
else if(apply_to ==
"profile") {
2060 for(
const config&
c : cfg_range) {
2061 if(!
c[
"remove"].to_bool()) {
2071 }
else if(apply_to ==
"new_attack") {
2075 for(
const auto [key, special] : specials.all_children_view()) {
2077 events.add_child(
"event", special_event);
2081 }
else if(apply_to ==
"remove_attacks") {
2084 }
else if(apply_to ==
"attack") {
2087 a->apply_modification(effect);
2089 for(
const auto [key, special] : specials.all_children_view()) {
2091 events.add_child(
"event", special_event);
2096 }
else if(apply_to ==
"hitpoints") {
2098 const std::string& increase_hp = effect[
"increase"];
2099 const std::string& increase_total = effect[
"increase_total"];
2100 const std::string& set_hp = effect[
"set"];
2101 const std::string& set_total = effect[
"set_total"];
2104 const bool violate_max = effect[
"violate_maximum"].to_bool();
2106 if(!set_hp.empty()) {
2107 if(set_hp.back() ==
'%') {
2114 if(!set_total.empty()) {
2115 if(set_total.back() ==
'%') {
2122 if(!increase_total.empty()) {
2130 if(effect[
"heal_full"].to_bool()) {
2134 if(!increase_hp.empty()) {
2140 LOG_UT <<
"resetting hp to max";
2147 }
else if(apply_to ==
"movement") {
2148 const bool apply_to_vision = effect[
"apply_to_vision"].to_bool(
true);
2157 const std::string& increase = effect[
"increase"];
2158 if(!increase.empty()) {
2168 if(apply_to_vision) {
2171 }
else if(apply_to ==
"vision") {
2177 const std::string& increase = effect[
"increase"];
2178 if(!increase.empty()) {
2183 }
else if(apply_to ==
"jamming") {
2184 const std::string& increase = effect[
"increase"];
2186 if(!increase.empty()) {
2191 }
else if(apply_to ==
"experience") {
2192 const std::string& increase = effect[
"increase"];
2193 const std::string&
set = effect[
"set"];
2196 if(
set.back() ==
'%') {
2203 if(increase.empty() ==
false) {
2206 }
else if(apply_to ==
"max_experience") {
2207 const std::string& increase = effect[
"increase"];
2208 const std::string&
set = effect[
"set"];
2210 if(
set.empty() ==
false) {
2211 if(
set.back() ==
'%') {
2218 if(increase.empty() ==
false) {
2223 }
else if(apply_to ==
"status") {
2224 const std::string& add = effect[
"add"];
2225 const std::string&
remove = effect[
"remove"];
2242 }
else if(apply_to ==
"zoc") {
2247 }
else if(apply_to ==
"new_ability") {
2251 for(
const auto [key, cfg] : ab_effect->all_children_view()) {
2255 events.add_child(
"event", event);
2261 }
else if(apply_to ==
"remove_ability") {
2263 for(
const auto [key, cfg] : ab_effect->all_children_view()) {
2270 if(
auto fab_effect = effect.
optional_child(
"experimental_filter_ability")) {
2274 }
else if(apply_to ==
"image_mod") {
2275 LOG_UT <<
"applying image_mod";
2276 std::string mod = effect[
"replace"];
2280 LOG_UT <<
"applying image_mod";
2281 mod = effect[
"add"].str();
2291 LOG_UT <<
"applying image_mod";
2292 }
else if(apply_to ==
"new_animation") {
2293 anim_comp_->apply_new_animation_effect(effect);
2294 }
else if(apply_to ==
"ellipse") {
2296 }
else if(apply_to ==
"halo") {
2298 }
else if(apply_to ==
"overlay") {
2299 const std::string& add = effect[
"add"];
2300 const std::string& replace = effect[
"replace"];
2301 const std::string&
remove = effect[
"remove"];
2313 if(add.empty() &&
remove.empty() && !replace.empty()) {
2316 }
else if(apply_to ==
"new_advancement") {
2317 const std::string& types = effect[
"types"];
2318 const bool replace = effect[
"replace"].to_bool(
false);
2321 if(!types.empty()) {
2326 std::copy(temp_advances.begin(), temp_advances.end(), std::back_inserter(
advances_to_));
2339 }
else if(apply_to ==
"remove_advancement") {
2340 const std::string& types = effect[
"types"];
2341 const std::string& amlas = effect[
"amlas"];
2346 for(
const std::string&
unit : temp_advances) {
2360 }
else if(apply_to ==
"alignment") {
2365 }
else if(apply_to ==
"max_attacks") {
2366 const std::string& increase = effect[
"increase"];
2368 if(!increase.empty()) {
2371 }
else if(apply_to ==
"recall_cost") {
2372 const std::string& increase = effect[
"increase"];
2373 const std::string&
set = effect[
"set"];
2378 if(
set.back() ==
'%') {
2385 if(!increase.empty()) {
2388 }
else if(effect[
"apply_to"] ==
"variation") {
2390 assert(base_type !=
nullptr);
2391 const std::string& variation_id = effect[
"name"];
2395 if(effect[
"heal_full"].to_bool(
false)) {
2399 WRN_UT <<
"unknown variation '" << variation_id <<
"' (name=) in [effect]apply_to=variation, ignoring";
2401 }
else if(effect[
"apply_to"] ==
"type") {
2402 std::string prev_type = effect[
"prev_type"];
2403 if(prev_type.empty()) {
2406 const std::string& new_type_id = effect[
"name"];
2411 if(effect[
"heal_full"].to_bool(
false)) {
2415 WRN_UT <<
"unknown type '" << new_type_id <<
"' (name=) in [effect]apply_to=type, ignoring";
2417 }
else if(effect[
"apply_to"] ==
"level") {
2418 const std::string& increase = effect[
"increase"];
2419 const std::string&
set = effect[
"set"];
2426 level_ = lexical_cast_default<int>(
set);
2429 if(!increase.empty()) {
2430 level_ += lexical_cast_default<int>(increase);
2442 bool generate_description = mod[
"generate_description"].to_bool(
true);
2444 config* target =
nullptr;
2446 if(no_add ==
false) {
2451 std::vector<t_string> effects_description;
2458 if(
auto afilter = effect.optional_child(
"filter")) {
2464 const std::string& apply_to = effect[
"apply_to"];
2465 int times = effect[
"times"].to_int(1);
2468 if(no_add && (apply_to ==
"type" || apply_to ==
"variation")) {
2472 if(effect[
"times"] ==
"per level") {
2473 if(effect[
"apply_to"] ==
"level") {
2474 WRN_UT <<
"[effect] times=per level is not allowed with apply_to=level, using default value of 1";
2485 std::string description_component;
2496 description += description_component;
2507 if(effect[
"times"] ==
"per level" && !times) {
2508 description =
VGETTEXT(
"$effect_description per level", {{
"effect_description", description}});
2511 if(!description.
empty()) {
2512 effects_description.push_back(description);
2518 const t_string& mod_description = mod[
"description"];
2519 if(!mod_description.
empty()) {
2520 description = mod_description;
2525 if(generate_description && !effects_description.empty()) {
2526 if(!mod_description.
empty()) {
2527 description +=
"\n";
2530 for(
const auto& desc_line : effects_description) {
2531 description += desc_line +
"\n";
2536 if(mod_type ==
"trait") {
2548 const t_string name = gender_specific_name.
empty() ? trait[
"name"] : gender_specific_name;
2600 return itor->second;
2605 static const std::string hides(
"hides");
2614 units_with_cache.push_back(
this);
2675 std::stringstream ss;
2693 std::string::size_type pos =
id_.find_last_of(
'-');
2694 if(pos != std::string::npos && pos+1 <
id_.size()
2695 &&
id_.find_first_not_of(
"0123456789", pos+1) == std::string::npos) {
2697 WRN_UT <<
"assigning new id to clone of generic unit " <<
id_;
2707 : u_(const_cast<
unit&>(u))
2708 , moves_(u.movement_left(true))
2724 DBG_UT <<
"The unit to be removed is not in the unit map.";
2805 }
catch(std::invalid_argument&
e) {
2806 WRN_UT <<
"Found invalid upkeep=\"" <<
e.what() <<
"\" in a unit";
2819 for(
const auto& a_ptr :
attacks_) {
2820 a_ptr->set_changed(
false);
2834 u.
write(unit_config);
2836 static const std::set<std::string_view> main_keys {
2843 "ignore_race_traits",
2844 "ignore_global_traits",
2861 for(
const std::string_view& main_key : main_keys) {
2862 wcfg[main_key] = unit_config[main_key];
2865 static const std::set<std::string_view> attack_keys {
2876 for(
const std::string_view& attack_key : attack_keys) {
2877 child[attack_key] = att[attack_key];
2910 static const std::set<std::string_view> child_keys {
2919 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
void remove_children(config_key_type key, const std::function< bool(const config &)> &p={})
Removes all children with tag key for which p returns true.
child_itors child_range(config_key_type key)
boost::iterator_range< const_attribute_iterator > const_attr_itors
std::size_t all_children_count() const
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 unit_type & get_gender_unit_type(const std::string &gender) const
Returns a gendered variant of this unit_type.
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 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.
bool remove_attack(const attack_ptr &atk)
Remove an attack from the unit.
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
attack_itors attacks()
Gets an iterator over this unit's attacks.
int resistance_against(const std::string &damage_name, bool attacker, const map_location &loc, const_attack_ptr weapon=nullptr, const const_attack_ptr &opp_weapon=nullptr) const
The unit's resistance against a given damage type.
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 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 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(const 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.
void apply_builtin_effect(const std::string &type, const config &effect)
Apply a builtin effect to the unit.
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.
int stoi(std::string_view str)
Same interface as std::stoi and meant as a drop in replacement, except:
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)
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
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
std::vector< t_string > combine_special_notes(const std::vector< t_string > &direct, const config &abilities, const const_attack_itors &attacks, const movetype &mt)
Common logic for unit_type::special_notes() and unit::special_notes().
unit_type_data unit_types
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)