Exemple #1
0
void unit_palette::draw_item(SDL_Rect& dstrect, const unit_type& u, std::stringstream& tooltip_text) {

	surface screen = gui_.video().getSurface();

	const std::string filename = u.image();
	surface image(image::get_image(filename));
	if(image == NULL) {
		tooltip_text << "IMAGE NOT FOUND\n";
		ERR_ED << "image for terrain: '" << filename << "' not found\n";
		image = image::get_image(game_config::images::missing);
		if (image == NULL) {
			ERR_ED << "Placeholder image not found\n";
			return;
		}
	}

	if(image->w != item_size_ || image->h != item_size_) {
		image.assign(scale_surface(image,
				item_size_, item_size_));
	}

	sdl_blit(image, NULL, screen, &dstrect);

	tooltip_text << u.type_name();
}
Exemple #2
0
int ai_default_recruitment_stage::compare_unit_types(const unit_type& a, const unit_type& b) const
{
    const int a_effectiveness_vs_b = average_resistance_against(b,a);
    const int b_effectiveness_vs_a = average_resistance_against(a,b);

    LOG_AI << "comparison of '" << a.id() << " vs " << b.id() << ": "
           << a_effectiveness_vs_b << " - " << b_effectiveness_vs_a << " = "
           << (a_effectiveness_vs_b - b_effectiveness_vs_a) << '\n';
    return a_effectiveness_vs_b - b_effectiveness_vs_a;
}
bool recruit_result::test_enough_gold(const team &my_team, const unit_type &type, bool)
{
	if (my_team.gold() < type.cost()) {
		set_error(E_NO_GOLD);
		return false;
	}
	return true;
}
void mouse_action_unit::set_unit_mouse_overlay(editor_display& disp, const unit_type& u)
{

	std::stringstream filename;
	filename << u.image() << "~RC(" << u.flag_rgb() << '>'
			<< team::get_side_color_index(disp.viewing_side()) << ')';

	surface image(image::get_image(filename.str()));
	Uint8 alpha = 196;
	//TODO don't hardcode
	int size = 72;
	//int size = image->w;
	int zoom = static_cast<int>(size * disp.get_zoom_factor());

	// Add the alpha factor and scale the image
	image = scale_surface(adjust_surface_alpha(image, alpha), zoom, zoom);
	disp.set_mouseover_hex_overlay(image);
}
Exemple #5
0
void tcore::generate_utype_tree_internal(const unit_type& current, std::vector<node>& advances_to, bool& to_branch)
{
	for (std::vector<node>::iterator it2 = advances_to.begin(); it2 != advances_to.end(); ++ it2) {
		const std::vector<std::string>& advances_to = it2->current->advances_to();
		if (std::find(advances_to.begin(), advances_to.end(), current.id()) != advances_to.end()) {
			it2->advances_to.push_back(node(&current));
			to_branch = true;
		}
		generate_utype_tree_internal(current, it2->advances_to, to_branch);
	}
}
Exemple #6
0
void ai_default_recruitment_stage::get_combat_score_vs(const unit_type& ut, const std::string &enemy_type_id, int &score, int &weighting, int hitpoints, int max_hitpoints) const
{
    const unit_type *enemy_info = unit_types.find(enemy_type_id);
    VALIDATE(enemy_info, "Unknown unit type : " + enemy_type_id + " while scoring units.");
    int weight = ut.cost();
    if ((hitpoints>0) && (max_hitpoints>0)) {
        weight = weight * hitpoints / max_hitpoints;
    }

    weighting += weight;
    score += compare_unit_types(ut, *enemy_info) * weight;
}
void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstream& tooltip_text) {

	std::stringstream filename;
	filename << u.image() << "~RC(" << u.flag_rgb() << '>'
	    	 << team::get_side_color_id(gui_.viewing_side()) << ')';

	image = image::get_image(filename.str());
	if(image == nullptr) {
		tooltip_text << "IMAGE NOT FOUND\n";
		ERR_ED << "image for unit type: '" << filename.str() << "' not found" << std::endl;
		image = image::get_image(game_config::images::missing);
		if(image == nullptr) {
			ERR_ED << "Placeholder image not found" << std::endl;
			return;
		}
	}

	if(image->w != item_size_ || image->h != item_size_) {
		image.assign(scale_surface(image, item_size_, item_size_));
	}

	tooltip_text << u.type_name();
}
Exemple #8
0
void unit_type_data::add_advancement(unit_type& to_unit) const
{
    const config& cfg = to_unit.get_cfg();

    for (const config &af : cfg.child_range("advancefrom"))
    {
        const std::string &from = af["unit"];
        int xp = af["experience"];

        unit_type_data::unit_type_map::iterator from_unit = types_.find(from);

		if (from_unit == types_.end()) {
			std::ostringstream msg;
			msg << "unit type '" << from << "' not found when resolving [advancefrom] tag for '"
				<< to_unit.log_id() << "'";
			throw config::error(msg.str());
		}

        // Fix up advance_from references
        from_unit->second.add_advancement(to_unit, xp);

        DBG_UT << "Added advancement ([advancefrom]) from " << from << " to " << to_unit.log_id() << "\n";
    }
}
Exemple #9
0
void show_unit_description(CVideo& video, const unit_type &t)
{
	std::string var_id = t.get_cfg()["variation_id"].str();
	if (var_id.empty())
		var_id = t.get_cfg()["variation_name"].str();
	bool hide_help = t.hide_help();
	bool use_variation = false;
	if (!var_id.empty()) {
		const unit_type *parent = unit_types.find(t.id());
		assert(parent);
		if (hide_help) {
			hide_help = parent->hide_help();
		} else {
			use_variation = true;
		}
	}

	if (use_variation)
		help::show_variation_help(video, t.id(), var_id, hide_help);
	else
		help::show_unit_help(video, t.id(), t.show_variations_in_help(), hide_help);
}
Exemple #10
0
void unit_type::add_advancement(const unit_type &to_unit,int xp)
{
	const std::string &to_id = to_unit.id_;

	// Add extra advancement path to this unit type
	LOG_CONFIG << "adding advancement from " << log_id() << " to " << to_unit.log_id() << "\n";
	if(std::find(advances_to_.begin(), advances_to_.end(), to_id) == advances_to_.end()) {
		advances_to_.push_back(to_id);
	} else {
		LOG_CONFIG << "advancement from " << log_id() << " to "
		           << to_unit.log_id() << " already known, ignoring.\n";
		return;
	}

	if(xp > 0) {
		//xp is 0 in case experience= wasn't given.
		if(!in_advancefrom_) {
			//This function is called for and only for an [advancefrom] tag in a unit_type referencing this unit_type.
			in_advancefrom_ = true;
			experience_needed_ = xp;
			DBG_UT << "Changing experience_needed from " << experience_needed_
			       << " to " << xp << " due to (first) [advancefrom] of "
			       << to_unit.log_id() << "\n";
		}
		else if(experience_needed_ > xp) {
			experience_needed_ = xp;
			DBG_UT << "Lowering experience_needed from " << experience_needed_
			       << " to " << xp << " due to (multiple, lower) [advancefrom] of "
			       << to_unit.log_id() << "\n";
		}
		else
			DBG_UT << "Ignoring experience_needed change from " << experience_needed_
			       << " to " << xp << " due to (multiple, higher) [advancefrom] of "
			       << to_unit.log_id() << "\n";
	}

	// Add advancements to gendered subtypes, if supported by to_unit
	for(int gender=0; gender<=1; ++gender) {
		if(gender_types_[gender] == nullptr) continue;
		if(to_unit.gender_types_[gender] == nullptr) {
			WRN_CF << to_unit.log_id() << " does not support gender " << gender << std::endl;
			continue;
		}
		LOG_CONFIG << "gendered advancement " << gender << ": ";
		gender_types_[gender]->add_advancement(*(to_unit.gender_types_[gender]),xp);
	}

	if ( cfg_.has_child("variation") ) {
		// Make sure the variations are created.
		unit_types.build_unit_type(*this, VARIATIONS);

		// Add advancements to variation subtypes.
		// Since these are still a rare and special-purpose feature,
		// we assume that the unit designer knows what they're doing,
		// and don't block advancements that would remove a variation.
		for(variations_map::iterator v=variations_.begin();
			v!=variations_.end(); ++v) {
			LOG_CONFIG << "variation advancement: ";
			v->second->add_advancement(to_unit,xp);
		}
	}
}
Exemple #11
0
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;
}
Exemple #12
0
	/// Makes sure the provided unit_type is built to the specified level.
	void build_unit_type(const unit_type & ut, unit_type::BUILD_STATUS status) const
	{ ut.build(status, movement_types_, races_, unit_cfg_->child_range("trait")); }
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());
	}
}
Exemple #14
0
void show_unit_description(const unit_type &t)
{
	help::show_unit_help(*resources::screen, t.id(), t.hide_help());
}