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 {
120 "block_dismiss_message",
154 auto cur_known = internalized_attrs.begin();
155 auto end_known = internalized_attrs.end();
157 while(cur_known != end_known) {
161 int comp = cur->first.compare(*cur_known);
163 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
176 WRN_UT <<
"Unknown attribute '" << cur->first <<
"' discarded.";
181 auto stats_storage_resetter(
unit& u,
bool clamp =
false)
218 if(type_id.empty()) {
221 std::string new_id = type_id;
230 const std::vector<unit_race::GENDER>& genders =
type.genders();
231 assert(genders.size() > 0);
233 if(random_gender ==
false || genders.size() == 1) {
234 return genders.front();
242 const std::string& gender = cfg[
"gender"];
243 if(!gender.empty()) {
252 : std::enable_shared_from_this<
unit>()
254 , advances_to_(o.advances_to_)
256 , type_name_(o.type_name_)
260 , underlying_id_(o.underlying_id_)
261 , undead_variation_(o.undead_variation_)
262 , variation_(o.variation_)
263 , hit_points_(o.hit_points_)
264 , max_hit_points_(o.max_hit_points_)
265 , experience_(o.experience_)
266 , max_experience_(o.max_experience_)
268 , recall_cost_(o.recall_cost_)
269 , canrecruit_(o.canrecruit_)
270 , recruit_list_(o.recruit_list_)
271 , alignment_(o.alignment_)
272 , flag_rgb_(o.flag_rgb_)
273 , image_mods_(o.image_mods_)
274 , unrenamable_(o.unrenamable_)
275 , dismissable_(o.dismissable_)
276 , dismiss_message_(o.dismiss_message_)
280 , movement_(o.movement_)
281 , max_movement_(o.max_movement_)
283 , jamming_(o.jamming_)
284 , movement_type_(o.movement_type_)
285 , hold_position_(o.hold_position_)
286 , end_turn_(o.end_turn_)
287 , resting_(o.resting_)
288 , attacks_left_(o.attacks_left_)
289 , max_attacks_(o.max_attacks_)
291 , known_boolean_states_(o.known_boolean_states_)
292 , variables_(o.variables_)
294 , filter_recall_(o.filter_recall_)
295 , emit_zoc_(o.emit_zoc_)
296 , overlays_(o.overlays_)
298 , attacks_(o.attacks_)
300 , trait_names_(o.trait_names_)
301 , trait_descriptions_(o.trait_descriptions_)
302 , trait_nonhidden_ids_(o.trait_nonhidden_ids_)
303 , unit_value_(o.unit_value_)
305 , interrupted_move_(o.interrupted_move_)
306 , is_fearless_(o.is_fearless_)
307 , is_healthy_(o.is_healthy_)
308 , modification_descriptions_(o.modification_descriptions_)
311 , modifications_(o.modifications_)
312 , abilities_(o.abilities_)
313 , advancements_(o.advancements_)
314 , description_(o.description_)
315 , special_notes_(o.special_notes_)
318 , ellipse_(o.ellipse_)
319 , random_traits_(o.random_traits_)
320 , generate_name_(o.generate_name_)
322 , profile_(o.profile_)
323 , small_profile_(o.small_profile_)
324 , changed_attributes_(o.changed_attributes_)
325 , invisibility_cache_()
334 : std::enable_shared_from_this<
unit>()
343 , undead_variation_()
356 , unrenamable_(false)
358 , dismiss_message_(
_(
"This unit cannot be dismissed."))
367 , hold_position_(false)
373 , known_boolean_states_()
383 , trait_descriptions_()
384 , trait_nonhidden_ids_()
387 , interrupted_move_()
388 , is_fearless_(false)
390 , modification_descriptions_()
401 , random_traits_(true)
402 , generate_name_(true)
404 , changed_attributes_(0)
405 , invisibility_cache_()
414 id_ = cfg[
"id"].str();
419 role_ = cfg[
"role"].str();
422 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();
724 if(!cfg[
"block_dismiss_message"].
blank()) {
730 if(!cfg[
"recall_cost"].
blank()) {
744 for(
const auto& anim :
anim_comp_->animations_) {
745 std::cout << anim.debug() << std::endl;
752 for(
auto& u : units_with_cache) {
753 u->clear_visibility_cache();
756 units_with_cache.clear();
766 facing_ = get_random_direction();
790 auto itor =
std::find(units_with_cache.begin(), units_with_cache.end(),
this);
792 if(itor != units_with_cache.end()) {
793 units_with_cache.erase(itor);
795 }
catch(
const std::exception &
e) {
796 ERR_UT <<
"Caught exception when destroying unit: " <<
e.what();
813 LOG_UT <<
"Generating a trait for unit type " <<
type().
log_id() <<
" with must_have_only " << must_have_only;
821 const std::string& tid =
t[
"id"];
822 bool already =
false;
823 for(
const config& mod : current_traits) {
824 if(mod[
"id"] == tid) {
833 const std::string& avl =
t[
"availability"];
834 if(avl ==
"musthave") {
845 std::vector<const config*> candidate_traits;
846 std::vector<std::string> temp_require_traits;
847 std::vector<std::string> temp_exclude_traits;
851 int nb_traits = current_traits.size();
853 for(; nb_traits < max_traits; ++nb_traits)
856 candidate_traits.clear();
859 const std::string& tid =
t[
"id"];
860 bool already =
false;
861 for(
const config& mod : current_traits) {
862 if(mod[
"id"] == tid) {
877 for(
const config& mod : current_traits) {
878 if (mod[
"exclude_traits"] !=
"") {
880 temp_exclude_traits.push_back(
c);
886 bool trait_req_met =
true;
887 for(
const std::string&
s : temp_require_traits) {
888 bool has_trait =
false;
889 for(
const config& mod : current_traits) {
894 trait_req_met =
false;
903 bool trait_exc_met =
true;
905 for(
const std::string&
s : temp_exclude_traits) {
906 bool has_exclusionary_trait =
false;
907 for(
const config& mod : current_traits) {
909 has_exclusionary_trait =
true;
912 has_exclusionary_trait =
true;
914 if(has_exclusionary_trait) {
915 trait_exc_met =
false;
923 const std::string& avl =
t[
"availability"];
926 if(!must_have_only && (!
can_recruit() || avl ==
"any")) {
927 candidate_traits.push_back(&
t);
931 if(candidate_traits.empty()) {
937 candidate_traits.
erase(candidate_traits.begin() + num);
946 std::vector<std::string> res;
951 res.push_back(mod[
"id"]);
953 if(mod_type ==
"advancement"){
955 res.push_back(mod[
"id"]);
965 if(
type ==
"advancement"){
981 auto ss = stats_storage_resetter(*
this,
true);
1004 if(!new_type.
usage().empty()) {
1009 if(!new_type.
ellipse().empty()) {
1052 return std::make_shared<attack_type>(atk);
1094 events.add_child(
"event", unit_event);
1099 events.add_child(
"event", ability_event);
1105 for(
const auto [key, special] : specials.all_children_view()) {
1107 events.add_child(
"event", special_event);
1150 return leader_crown_path;
1160 const double unit_energy = max_hitpoints > 0
1161 ?
static_cast<double>(hitpoints) / max_hitpoints
1164 if(1.0 == unit_energy) {
1165 return {33, 225, 0};
1166 }
else if(unit_energy > 1.0) {
1167 return {100, 255, 100};
1168 }
else if(unit_energy >= 0.75) {
1169 return {170, 255, 0};
1170 }
else if(unit_energy >= 0.5) {
1171 return {255, 175, 0};
1172 }
else if(unit_energy >= 0.25) {
1173 return {255, 155, 0};
1202 return {255, 255, 255};
1203 }
else if(mid_advance) {
1204 return {150, 255, 255};
1205 }
else if(far_advance) {
1206 return {0, 205, 205};
1208 }
else if(has_amla) {
1210 return {225, 0, 255};
1211 }
else if(mid_advance) {
1212 return {169, 30, 255};
1213 }
else if(far_advance) {
1214 return {139, 0, 237};
1216 return {170, 0, 255};
1220 return {0, 160, 225};
1225 bool major_amla =
false;
1226 bool has_amla =
false;
1228 major_amla |= adv[
"major_amla"].to_bool();
1243 std::vector<std::string> result;
1246 result.push_back(adv_type->type_name());
1248 WRN_UT <<
"unknown unit in advances_to list of type "
1281 if(goal_dur.empty()) {
1283 return !mod_dur.empty() && mod_dur !=
"forever";
1286 return mod_dur == goal_dur;
1292 const unit_type* rebuild_from =
nullptr;
1294 for(
const auto& mod_name : ModificationTypes) {
1307 else if(rebuild_from ==
nullptr) {
1308 rebuild_from = &
type();
1316 if(rebuild_from !=
nullptr) {
1379 std::set<std::string> all_states =
states_;
1382 all_states.insert(state.first);
1387 if(all_states.count(
"undrainable") && all_states.count(
"unpoisonable") && all_states.count(
"unplagueable")) {
1388 all_states.insert(
"not_living");
1398 return get_state(known_boolean_state_id);
1402 if(state ==
"not_living") {
1435 if(
p.second == state) {
1443 {
"slowed", STATE_SLOWED},
1444 {
"poisoned", STATE_POISONED},
1445 {
"petrified", STATE_PETRIFIED},
1446 {
"uncovered", STATE_UNCOVERED},
1447 {
"not_moved", STATE_NOT_MOVED},
1448 {
"unhealable", STATE_UNHEALABLE},
1449 {
"guardian", STATE_GUARDIAN},
1450 {
"invulnerable", STATE_INVULNERABLE},
1458 set_state(known_boolean_state_id, value);
1463 if(state ==
"not_living") {
1479 if(cfg[
"id"] == ability) {
1492 if(
i->cfg[
"id"] == ability) {
1515 for(
const auto& a_ptr :
attacks_) {
1516 if(a_ptr->get_changed()) {
1527 auto write_subtag = [&](
const std::string& key,
const config& child)
1531 if(!child.empty()) {
1552 cfg.
add_child(
"special_note")[
"note"] = note;
1557 cfg[
"halo"] = *
halo_;
1584 cfg[
"side"] =
side_;
1597 cfg[
"role"] =
role_;
1600 for(
const std::string& state :
get_states()) {
1601 status_flags[state] =
true;
1606 write_subtag(
"status", status_flags);
1614 cfg[
"overlays"] =
"";
1616 cfg[
"name"] =
name_;
1621 cfg[
"canrecruit"] =
true;
1683 if(!advancement.empty()) {
1684 cfg.
add_child(
"advancement", advancement);
1712 return utils::holds_alternative<upkeep_loyal>(
upkeep_);
1719 overlays_.push_back(
"misc/loyal-icon.png");
1733 if(!defense_abilities.
empty()) {
1743 const std::string& apply_to = cfg[
"apply_to"];
1744 if(!apply_to.empty()) {
1745 if(damage_name != apply_to) {
1746 if(apply_to.find(
',') != std::string::npos &&
1747 apply_to.find(damage_name) != std::string::npos) {
1748 const std::vector<std::string>& vals =
utils::split(apply_to);
1749 if(
std::find(vals.begin(),vals.end(),damage_name) == vals.end()) {
1772 if(!resistance_list.
empty()) {
1783 if(!(!cfg.
has_attribute(
"active_on") || (attacker && cfg[
"active_on"] ==
"offense") || (!attacker && cfg[
"active_on"] ==
"defense"))) {
1793 return opp_weapon->effective_damage_type().second;
1804 std::map<std::string,std::string> temp;
1823 const std::string&
image = adv[
"image"];
1831 const std::string& tt = adv[
"description"];
1844 std::vector<std::pair<std::string, std::string>> temp;
1845 std::pair<std::string, std::string> icon;
1848 icon.first = adv[
"icon"].str();
1849 icon.second = adv[
"description"].str();
1851 for(
unsigned j = 0, j_count =
modification_count(
"advancement", adv[
"id"]); j < j_count; ++j) {
1852 temp.push_back(icon);
1861 std::vector<config> res;
1863 if(adv[
"strict_amla"].to_bool() && !
advances_to_.empty()) {
1866 if(
auto filter = adv.optional_child(
"filter")) {
1872 if(
modification_count(
"advancement", adv[
"id"]) >=
static_cast<unsigned>(adv[
"max_times"].to_int(1))) {
1876 std::vector<std::string> temp_require =
utils::split(adv[
"require_amla"]);
1877 std::vector<std::string> temp_exclude =
utils::split(adv[
"exclude_amla"]);
1879 if(temp_require.empty() && temp_exclude.empty()) {
1884 std::sort(temp_require.begin(), temp_require.end());
1885 std::sort(temp_exclude.begin(), temp_exclude.end());
1887 std::vector<std::string> uniq_require, uniq_exclude;
1889 std::unique_copy(temp_require.begin(), temp_require.end(), std::back_inserter(uniq_require));
1890 std::unique_copy(temp_exclude.begin(), temp_exclude.end(), std::back_inserter(uniq_exclude));
1892 bool exclusion_found =
false;
1893 for(
const std::string&
s : uniq_exclude) {
1894 int max_num = std::count(temp_exclude.begin(), temp_exclude.end(),
s);
1896 if(mod_num >= max_num) {
1897 exclusion_found =
true;
1902 if(exclusion_found) {
1906 bool requirements_done =
true;
1907 for(
const std::string&
s : uniq_require) {
1908 int required_num = std::count(temp_require.begin(), temp_require.end(),
s);
1910 if(required_num > mod_num) {
1911 requirements_done =
false;
1916 if(requirements_done) {
1944 std::size_t res = 0;
1946 if(item[
"id"] ==
id) {
1952 if(mod_type ==
"advancement") {
1960 "alignment",
"attack",
"defense",
"ellipse",
"experience",
"fearless",
1961 "halo",
"healthy",
"hitpoints",
"image_mod",
"jamming",
"jamming_costs",
"level",
1962 "loyal",
"max_attacks",
"max_experience",
"movement",
"movement_costs",
1963 "new_ability",
"new_advancement",
"new_animation",
"new_attack",
"overlay",
"profile",
1964 "recall_cost",
"remove_ability",
"remove_advancement",
"remove_attacks",
"resistance",
1965 "status",
"type",
"variation",
"vision",
"vision_costs",
"zoc"
1970 if(apply_to ==
"attack") {
1971 std::vector<t_string> attack_names;
1975 bool affected = a->describe_modification(effect, &desc);
1976 if(affected && !desc.empty()) {
1977 attack_names.emplace_back(a->name(),
"wesnoth-units");
1980 if(!attack_names.empty()) {
1983 symbols[
"effect_description"] = desc;
1984 return VGETTEXT(
"$attack_list|: $effect_description", symbols);
1986 }
else if(apply_to ==
"hitpoints") {
1987 const std::string& increase_total = effect[
"increase_total"];
1988 if(!increase_total.empty()) {
1990 "<span color=\"$color\">$number_or_percent</span> HP",
1991 {{
"number_or_percent",
utils::print_modifier(increase_total)}, {
"color", increase_total[0] ==
'-' ?
"#f00" :
"#0f0"}});
1994 const std::string& increase = effect[
"increase"];
1995 if(increase.empty()) {
1998 if(apply_to ==
"movement") {
2000 "<span color=\"$color\">$number_or_percent</span> move",
2001 "<span color=\"$color\">$number_or_percent</span> moves",
2003 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2004 }
else if(apply_to ==
"vision") {
2006 "<span color=\"$color\">$number_or_percent</span> vision",
2007 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2008 }
else if(apply_to ==
"jamming") {
2010 "<span color=\"$color\">$number_or_percent</span> jamming",
2011 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2012 }
else if(apply_to ==
"max_experience") {
2015 "<span color=\"$color\">$number_or_percent</span> XP to advance",
2016 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
2017 }
else if(apply_to ==
"max_attacks") {
2019 "<span color=\"$color\">$number_or_percent</span> attack per turn",
2020 "<span color=\"$color\">$number_or_percent</span> attacks per turn",
2022 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#f00" :
"#0f0"}});
2023 }
else if(apply_to ==
"recall_cost") {
2026 "<span color=\"$color\">$number_or_percent</span> cost to recall",
2027 {{
"number_or_percent",
utils::print_modifier(increase)}, {
"color", increase[0] ==
'-' ?
"#0f0" :
"#f00"}});
2037 if(apply_to ==
"fearless") {
2040 }
else if(apply_to ==
"healthy") {
2043 }
else if(apply_to ==
"profile") {
2057 for(
const config&
c : cfg_range) {
2058 if(!
c[
"remove"].to_bool()) {
2068 }
else if(apply_to ==
"new_attack") {
2072 for(
const auto [key, special] : specials.all_children_view()) {
2074 events.add_child(
"event", special_event);
2078 }
else if(apply_to ==
"remove_attacks") {
2081 }
else if(apply_to ==
"attack") {
2084 a->apply_modification(effect);
2086 for(
const auto [key, special] : specials.all_children_view()) {
2088 events.add_child(
"event", special_event);
2093 }
else if(apply_to ==
"hitpoints") {
2095 const std::string& increase_hp = effect[
"increase"];
2096 const std::string& increase_total = effect[
"increase_total"];
2097 const std::string& set_hp = effect[
"set"];
2098 const std::string& set_total = effect[
"set_total"];
2101 const bool violate_max = effect[
"violate_maximum"].to_bool();
2103 if(!set_hp.empty()) {
2104 if(set_hp.back() ==
'%') {
2111 if(!set_total.empty()) {
2112 if(set_total.back() ==
'%') {
2119 if(!increase_total.empty()) {
2127 if(effect[
"heal_full"].to_bool()) {
2131 if(!increase_hp.empty()) {
2137 LOG_UT <<
"resetting hp to max";
2144 }
else if(apply_to ==
"movement") {
2145 const bool apply_to_vision = effect[
"apply_to_vision"].to_bool(
true);
2154 const std::string& increase = effect[
"increase"];
2155 if(!increase.empty()) {
2165 if(apply_to_vision) {
2168 }
else if(apply_to ==
"vision") {
2174 const std::string& increase = effect[
"increase"];
2175 if(!increase.empty()) {
2180 }
else if(apply_to ==
"jamming") {
2181 const std::string& increase = effect[
"increase"];
2183 if(!increase.empty()) {
2188 }
else if(apply_to ==
"experience") {
2189 const std::string& increase = effect[
"increase"];
2190 const std::string&
set = effect[
"set"];
2193 if(
set.back() ==
'%') {
2200 if(increase.empty() ==
false) {
2203 }
else if(apply_to ==
"max_experience") {
2204 const std::string& increase = effect[
"increase"];
2205 const std::string&
set = effect[
"set"];
2207 if(
set.empty() ==
false) {
2208 if(
set.back() ==
'%') {
2215 if(increase.empty() ==
false) {
2220 }
else if(apply_to ==
"status") {
2221 const std::string& add = effect[
"add"];
2222 const std::string&
remove = effect[
"remove"];
2239 }
else if(apply_to ==
"zoc") {
2244 }
else if(apply_to ==
"new_ability") {
2248 for(
const auto [key, cfg] : ab_effect->all_children_view()) {
2252 events.add_child(
"event", event);
2258 }
else if(apply_to ==
"remove_ability") {
2260 for(
const auto [key, cfg] : ab_effect->all_children_view()) {
2267 if(
auto fab_effect = effect.
optional_child(
"experimental_filter_ability")) {
2271 }
else if(apply_to ==
"image_mod") {
2272 LOG_UT <<
"applying image_mod";
2273 std::string mod = effect[
"replace"];
2277 LOG_UT <<
"applying image_mod";
2278 mod = effect[
"add"].str();
2288 LOG_UT <<
"applying image_mod";
2289 }
else if(apply_to ==
"new_animation") {
2290 anim_comp_->apply_new_animation_effect(effect);
2291 }
else if(apply_to ==
"ellipse") {
2293 }
else if(apply_to ==
"halo") {
2295 }
else if(apply_to ==
"overlay") {
2296 const std::string& add = effect[
"add"];
2297 const std::string& replace = effect[
"replace"];
2298 const std::string&
remove = effect[
"remove"];
2310 if(add.empty() &&
remove.empty() && !replace.empty()) {
2313 }
else if(apply_to ==
"new_advancement") {
2314 const std::string& types = effect[
"types"];
2315 const bool replace = effect[
"replace"].to_bool(
false);
2318 if(!types.empty()) {
2323 std::copy(temp_advances.begin(), temp_advances.end(), std::back_inserter(
advances_to_));
2336 }
else if(apply_to ==
"remove_advancement") {
2337 const std::string& types = effect[
"types"];
2338 const std::string& amlas = effect[
"amlas"];
2343 for(
const std::string&
unit : temp_advances) {
2357 }
else if(apply_to ==
"alignment") {
2362 }
else if(apply_to ==
"max_attacks") {
2363 const std::string& increase = effect[
"increase"];
2365 if(!increase.empty()) {
2368 }
else if(apply_to ==
"recall_cost") {
2369 const std::string& increase = effect[
"increase"];
2370 const std::string&
set = effect[
"set"];
2375 if(
set.back() ==
'%') {
2382 if(!increase.empty()) {
2385 }
else if(effect[
"apply_to"] ==
"variation") {
2387 assert(base_type !=
nullptr);
2388 const std::string& variation_id = effect[
"name"];
2392 if(effect[
"heal_full"].to_bool(
false)) {
2396 WRN_UT <<
"unknown variation '" << variation_id <<
"' (name=) in [effect]apply_to=variation, ignoring";
2398 }
else if(effect[
"apply_to"] ==
"type") {
2399 std::string prev_type = effect[
"prev_type"];
2400 if(prev_type.empty()) {
2403 const std::string& new_type_id = effect[
"name"];
2408 if(effect[
"heal_full"].to_bool(
false)) {
2412 WRN_UT <<
"unknown type '" << new_type_id <<
"' (name=) in [effect]apply_to=type, ignoring";
2414 }
else if(effect[
"apply_to"] ==
"level") {
2415 const std::string& increase = effect[
"increase"];
2416 const std::string&
set = effect[
"set"];
2423 level_ = lexical_cast_default<int>(
set);
2426 if(!increase.empty()) {
2427 level_ += lexical_cast_default<int>(increase);
2439 bool generate_description = mod[
"generate_description"].to_bool(
true);
2441 config* target =
nullptr;
2443 if(no_add ==
false) {
2448 std::vector<t_string> effects_description;
2455 if(
auto afilter = effect.optional_child(
"filter")) {
2461 const std::string& apply_to = effect[
"apply_to"];
2462 int times = effect[
"times"].to_int(1);
2465 if(no_add && (apply_to ==
"type" || apply_to ==
"variation")) {
2469 if(effect[
"times"] ==
"per level") {
2470 if(effect[
"apply_to"] ==
"level") {
2471 WRN_UT <<
"[effect] times=per level is not allowed with apply_to=level, using default value of 1";
2482 std::string description_component;
2493 description += description_component;
2504 if(effect[
"times"] ==
"per level" && !times) {
2505 description =
VGETTEXT(
"$effect_description per level", {{
"effect_description", description}});
2508 if(!description.
empty()) {
2509 effects_description.push_back(description);
2515 const t_string& mod_description = mod[
"description"];
2516 if(!mod_description.
empty()) {
2517 description = mod_description;
2522 if(generate_description && !effects_description.empty()) {
2523 if(!mod_description.
empty()) {
2524 description +=
"\n";
2527 for(
const auto& desc_line : effects_description) {
2528 description += desc_line +
"\n";
2533 if(mod_type ==
"trait") {
2545 const t_string name = gender_specific_name.
empty() ? trait[
"name"] : gender_specific_name;
2597 return itor->second;
2602 static const std::string hides(
"hides");
2611 units_with_cache.push_back(
this);
2672 std::stringstream ss;
2690 std::string::size_type pos =
id_.find_last_of(
'-');
2691 if(pos != std::string::npos && pos+1 <
id_.size()
2692 &&
id_.find_first_not_of(
"0123456789", pos+1) == std::string::npos) {
2694 WRN_UT <<
"assigning new id to clone of generic unit " <<
id_;
2704 : u_(const_cast<
unit&>(u))
2705 , moves_(u.movement_left(true))
2721 DBG_UT <<
"The unit to be removed is not in the unit map.";
2811 }
catch(std::invalid_argument&
e) {
2812 WRN_UT <<
"Found invalid upkeep=\"" <<
e.what() <<
"\" in a unit";
2825 for(
const auto& a_ptr :
attacks_) {
2826 a_ptr->set_changed(
false);
2840 u.
write(unit_config);
2842 static const std::set<std::string_view> main_keys {
2849 "ignore_race_traits",
2850 "ignore_global_traits",
2867 for(
const std::string_view& main_key : main_keys) {
2868 wcfg[main_key] = unit_config[main_key];
2871 static const std::set<std::string_view> attack_keys {
2882 for(
const std::string_view& attack_key : attack_keys) {
2883 child[attack_key] = att[attack_key];
2916 static const std::set<std::string_view> child_keys {
2925 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.
static std::string _(const char *str)
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_
t_string dismiss_message_
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.
double xp_bar_scaling() const
The factor by which the XP bar should be scaled.
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'.
double hp_bar_scaling() const
The factor by which the HP bar should be scaled.
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)