当前位置: 首页>>代码示例>>C++>>正文


C++ unit_type::has_ability_by_id方法代码示例

本文整理汇总了C++中unit_type::has_ability_by_id方法的典型用法代码示例。如果您正苦于以下问题:C++ unit_type::has_ability_by_id方法的具体用法?C++ unit_type::has_ability_by_id怎么用?C++ unit_type::has_ability_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在unit_type的用法示例。


在下文中一共展示了unit_type::has_ability_by_id方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: average_resistance_against

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;
}
开发者ID:rasata,项目名称:wesnoth,代码行数:77,代码来源:ai.cpp


注:本文中的unit_type::has_ability_by_id方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。