int ai_default_recruitment_stage::average_resistance_against(const unit_type& a, const unit_type& b) const { int weighting_sum = 0, defense = 0; const std::map<t_translation::t_terrain, size_t>& terrain = resources::gameboard->map().get_weighted_terrain_frequencies(); for (std::map<t_translation::t_terrain, size_t>::const_iterator j = terrain.begin(), j_end = terrain.end(); j != j_end; ++j) { // Use only reachable tiles when computing the average defense. if (a.movement_type().movement_cost(j->first) < movetype::UNREACHABLE) { defense += a.movement_type().defense_modifier(j->first) * j->second; weighting_sum += j->second; } } if (weighting_sum == 0) { // This unit can't move on this map, so just get the average weighted // of all available terrains. This still is a kind of silly // since the opponent probably can't recruit this unit and it's a static unit. for (std::map<t_translation::t_terrain, size_t>::const_iterator jj = terrain.begin(), jj_end = terrain.end(); jj != jj_end; ++jj) { defense += a.movement_type().defense_modifier(jj->first) * jj->second; weighting_sum += jj->second; } } if(weighting_sum != 0) { defense /= weighting_sum; } else { ERR_AI << "The weighting sum is 0 and is ignored." << std::endl; } LOG_AI << "average defense of '" << a.id() << "': " << defense << "\n"; int sum = 0, weight_sum = 0; // calculation of the average damage taken bool steadfast = a.has_ability_by_id("steadfast"); bool poisonable = !a.musthave_status("unpoisonable"); const std::vector<attack_type>& attacks = b.attacks(); for (std::vector<attack_type>::const_iterator i = attacks.begin(), i_end = attacks.end(); i != i_end; ++i) { int resistance = a.movement_type().resistance_against(*i); // Apply steadfast resistance modifier. if (steadfast && resistance < 100) resistance = std::max<int>(resistance * 2 - 100, 50); // Do not look for filters or values, simply assume 70% if CTH is customized. int cth = i->get_special_bool("chance_to_hit", true) ? 70 : defense; int weight = i->damage() * i->num_attacks(); // if cth == 0 the division will do 0/0 so don't execute this part if (poisonable && cth != 0 && i->get_special_bool("poison", true)) { // Compute the probability of not poisoning the unit. int prob = 100; for (int j = 0; j < i->num_attacks(); ++j) prob = prob * (100 - cth); // Assume poison works one turn. weight += game_config::poison_amount * (100 - prob) / 100; } sum += cth * resistance * weight * weight; // average damage * weight weight_sum += weight; } // normalize by HP sum /= std::max<int>(1,std::min<int>(a.hitpoints(),1000)); // avoid values really out of range // Catch division by zero here if the attacking unit // has zero attacks and/or zero damage. // If it has no attack at all, the ai shouldn't prefer // that unit anyway. if (weight_sum == 0) { return sum; } return sum/weight_sum; }
void tunit_preview_pane::set_displayed_type(const unit_type& type) { // Sets the current type id for the profile button callback to use current_type_ = type.id(); if(icon_type_) { std::string mods; if(resources::controller) { mods = "~RC(" + type.flag_rgb() + ">" + team::get_side_color_index(resources::controller->current_side()) + ")"; } mods += "~SCALE_INTO_SHARP(144,144)" + image_mods_; icon_type_->set_label((type.icon().empty() ? type.image() : type.icon()) + mods); } if(label_name_) { label_name_->set_label("<big>" + type.type_name() + "</big>"); label_name_->set_use_markup(true); } if(label_level_) { std::string l_str = vgettext("Lvl $lvl", {{"lvl", std::to_string(type.level())}}); label_level_->set_label("<b>" + l_str + "</b>"); label_level_->set_use_markup(true); } if(icon_race_) { icon_race_->set_label("icons/unit-groups/race_" + type.race_id() + "_30.png"); icon_race_->set_tooltip(type.race()->name(type.genders().front())); } if(icon_alignment_) { const std::string& alignment_name = type.alignment().to_string(); icon_alignment_->set_label("icons/alignments/alignment_" + alignment_name + "_30.png"); icon_alignment_->set_tooltip(unit_type::alignment_description( type.alignment(), type.genders().front())); } if(tree_details_) { std::stringstream str; str << "<small>"; str << "<span color='#21e100'>" << "<b>" << _("HP: ") << "</b>" << type.hitpoints() << "</span>" << " | "; str << "<span color='#00a0e1'>" << "<b>" << _("XP: ") << "</b>" << type.experience_needed() << "</span>" << " | "; str << "<b>" << _("MP: ") << "</b>" << type.movement(); str << "</small>"; tree_details_->clear(); add_name_tree_node( tree_details_->get_root_node(), "item", str.str() ); // Print trait details { ttree_view_node* header_node = nullptr; for(const auto& tr : type.possible_traits()) { t_string name = tr[type.genders().front() == unit_race::FEMALE ? "female_name" : "male_name"]; if (tr["availability"] != "musthave" || name.empty()) { continue; } if (header_node == nullptr) { header_node = &add_name_tree_node( tree_details_->get_root_node(), "header", "<b>" + _("Traits") + "</b>" ); } add_name_tree_node( *header_node, "item", name ); } } // Print ability details if(!type.abilities().empty()) { auto& header_node = add_name_tree_node( tree_details_->get_root_node(), "header", "<b>" + _("Abilities") + "</b>" ); for(const auto& ab : zip(type.abilities() , type.ability_tooltips())) { add_name_tree_node( header_node, "item", boost::get<0>(ab), boost::get<1>(ab) ); } } print_attack_details(type.attacks(), tree_details_->get_root_node()); } }