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


C++ unit类代码示例

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


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

示例1:

/**
 * Convenience constructor for when an event has a unit, saving the caller
 * the need to explicitly get the location and underlying ID.
 */
entity_location::entity_location(const unit &u)
	: map_location(u.get_location())
	, id_(u.underlying_id())
	, filter_loc_(*this)
{}
开发者ID:doofus-01,项目名称:wesnoth,代码行数:9,代码来源:entity_location.cpp

示例2: BOOST_FOREACH

	BOOST_FOREACH (unit & i, units_) {
		if (i.side() == player_num) {
			i.new_turn();
		}
	}
开发者ID:sunny975,项目名称:wesnoth,代码行数:5,代码来源:game_board.cpp

示例3: recall_unit

void recall_unit(const unit& u)
{
	stats& s = get_stats(u.side_id());
	s.recalls[u.type_id()]++;
	s.recall_cost += u.cost();
}
开发者ID:aelthwin,项目名称:Battle-for-Wesnoth--Zombie-Edition,代码行数:6,代码来源:statistics.cpp

示例4: unit_movement_resetter

	unit_movement_resetter(unit& u, bool operate=true) : u_(u), moves_(u.movement_)
	{
		if(operate) {
			u.movement_ = u.total_movement();
		}
	}
开发者ID:dodikk,项目名称:iWesnoth,代码行数:6,代码来源:unit.hpp

示例5: FAIL

bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_location& loc, const unit* u2) const
{
	if (!vcfg["name"].blank() && vcfg["name"].t_str() != u.name()) {
		return false;
	}

	if (!vcfg["id"].empty()) {
		std::vector<std::string> id_list = utils::split(vcfg["id"]);
		if (std::find(id_list.begin(), id_list.end(), u.id()) == id_list.end()) {
			return false;
		}
	}

	// Allow 'speaker' as an alternative to id, since people use it so often
	if (!vcfg["speaker"].blank() && vcfg["speaker"].str() != u.id()) {
		return false;
	}

	if (vcfg.has_child("filter_location")) {
		if (vcfg.count_children("filter_location") > 1) {
			FAIL("Encountered multiple [filter_location] children of a standard unit filter. "
				 "This is not currently supported and in all versions of wesnoth would have "
				 "resulted in the later children being ignored. You must use [and] or similar "
				 "to achieve the desired result.");
		}
		terrain_filter filt(vcfg.child("filter_location"), &fc_, use_flat_tod_);
		if (!filt.match(loc)) {
			return false;
		}
	}

	if(vcfg.has_child("filter_side")) {
		if (vcfg.count_children("filter_side") > 1) {
			FAIL("Encountered multiple [filter_side] children of a standard unit filter. "
				 "This is not currently supported and in all versions of wesnoth would have "
				 "resulted in the later children being ignored. You must use [and] or similar "
				 "to achieve the desired result.");
		}
		side_filter filt(vcfg.child("filter_side"), &fc_);
		if(!filt.match(u.side()))
			return false;
	}

	// Also allow filtering on location ranges outside of the location filter
	if (!vcfg["x"].blank() || !vcfg["y"].blank()){
		if(vcfg["x"] == "recall" && vcfg["y"] == "recall") {
			//locations on the map are considered to not be on a recall list
			if (fc_.get_disp_context().map().on_board(loc))
			{
				return false;
			}
		} else if(vcfg["x"].empty() && vcfg["y"].empty()) {
			return false;
		} else if(!loc.matches_range(vcfg["x"], vcfg["y"])) {
			return false;
		}
	}

	// The type could be a comma separated list of types
	if (!vcfg["type"].empty()) {
		std::vector<std::string> types = utils::split(vcfg["type"]);
		if (std::find(types.begin(), types.end(), u.type_id()) == types.end()) {
			return false;
		}
	}

	// The variation_type could be a comma separated list of types
	if (!vcfg["variation"].empty())
	{
		std::vector<std::string> types = utils::split(vcfg["variation"]);
		if (std::find(types.begin(), types.end(), u.variation()) == types.end()) {
			return false;
		}
	}

	// The has_variation_type could be a comma separated list of types
	if (!vcfg["has_variation"].empty())
	{
		bool match = false;
		// If this unit is a variation itself then search in the base unit's variations.
		const unit_type* const type = u.variation().empty() ? &u.type() : unit_types.find(u.type().base_id());
		assert(type);

		for (const std::string& variation_id : utils::split(vcfg["has_variation"])) {
			if (type->has_variation(variation_id)) {
				match = true;
				break;
			}
		}
		if (!match) return false;
	}

	if (!vcfg["ability"].empty())
	{
		bool match = false;

		for (const std::string& ability_id : utils::split(vcfg["ability"])) {
			if (u.has_ability_by_id(ability_id)) {
				match = true;
				break;
//.........这里部分代码省略.........
开发者ID:CliffsDover,项目名称:wesnoth,代码行数:101,代码来源:filter.cpp

示例6: VALIDATE

/** @todo FIXME: Hand previous defender unit in here. */
int battle_context::choose_defender_weapon(const unit& attacker,
		const unit& defender,
		unsigned attacker_weapon,
		const unit_map& units,
		const map_location& attacker_loc,
		const map_location& defender_loc,
		const combatant* prev_def)
{
	VALIDATE(attacker_weapon < attacker.attacks().size(), _("An invalid attacker weapon got selected."));

	const attack_type& att = attacker.attacks()[attacker_weapon];
	std::vector<unsigned int> choices;

	// What options does defender have?
	unsigned int i;

	for(i = 0; i < defender.attacks().size(); ++i) {
		const attack_type& def = defender.attacks()[i];

		if(def.range() == att.range() && def.defense_weight() > 0) {
			choices.push_back(i);
		}
	}

	if(choices.empty()) {
		return -1;
	}

	if(choices.size() == 1) {
		const battle_context_unit_stats def_stats(
				defender, defender_loc, choices[0], false, attacker, attacker_loc, att.shared_from_this(), units);

		return (def_stats.disable) ? -1 : choices[0];
	}

	// Multiple options:
	// First pass : get the best weight and the minimum simple rating for this weight.
	// simple rating = number of blows * damage per blows (resistance taken in account) * cth * weight
	// Eligible attacks for defense should have a simple rating greater or equal to this weight.

	int min_rating = 0;
	{
		double max_weight = 0.0;

		for(i = 0; i < choices.size(); ++i) {
			const attack_type& def = defender.attacks()[choices[i]];

			if(def.defense_weight() >= max_weight) {
				const battle_context_unit_stats def_stats(defender, defender_loc, choices[i], false, attacker,
						attacker_loc, att.shared_from_this(), units);

				if(def_stats.disable) {
					continue;
				}

				max_weight = def.defense_weight();
				int rating = static_cast<int>(
						def_stats.num_blows * def_stats.damage * def_stats.chance_to_hit * def.defense_weight());

				if(def.defense_weight() > max_weight || rating < min_rating) {
					min_rating = rating;
				}
			}
		}
	}

	// Multiple options: simulate them, save best.
	for(i = 0; i < choices.size(); ++i) {
		const attack_type& def = defender.attacks()[choices[i]];

		std::unique_ptr<battle_context_unit_stats> att_stats(new battle_context_unit_stats(
				attacker, attacker_loc, attacker_weapon, true, defender, defender_loc, def.shared_from_this(), units));

		std::unique_ptr<battle_context_unit_stats> def_stats(new battle_context_unit_stats(
				defender, defender_loc, choices[i], false, attacker, attacker_loc, att.shared_from_this(), units));

		if(def_stats->disable) {
			continue;
		}

		std::unique_ptr<combatant> att_comb(new combatant(*att_stats));
		std::unique_ptr<combatant> def_comb(new combatant(*def_stats, prev_def));

		att_comb->fight(*def_comb);

		int simple_rating = static_cast<int>(
				def_stats->num_blows * def_stats->damage * def_stats->chance_to_hit * def.defense_weight());

		if(simple_rating >= min_rating &&
			(!attacker_combatant_ || better_combat(*def_comb, *att_comb, *defender_combatant_, *attacker_combatant_, 1.0))
		) {
			attacker_combatant_ = std::move(att_comb);
			defender_combatant_ = std::move(def_comb);
			attacker_stats_ = std::move(att_stats);
			defender_stats_ = std::move(def_stats);
		}
	}

	return defender_stats_ ? defender_stats_->attack_num : -1;
//.........这里部分代码省略.........
开发者ID:fluffbeast,项目名称:wesnoth-old,代码行数:101,代码来源:attack.cpp

示例7: find_routes

static void find_routes(const gamemap& map, const unit_map& /*units*/,
		const unit& u, const map_location& loc,
		int move_left, pathfind::paths::dest_vect &destinations,
		std::vector<team> const &teams,
		bool force_ignore_zocs, bool allow_teleport, int turns_left,
		const team &viewing_team,
		bool see_all, bool ignore_units)
{
	const team& current_team = teams[u.side() - 1];
	pathfind::teleport_map teleports;
	if (allow_teleport) {
	  teleports = pathfind::get_teleport_locations(u, viewing_team, see_all, ignore_units);
	}

	const int total_movement = u.total_movement();

	search_counter += 2;
	if (search_counter == 0) search_counter = 2;

	static std::vector<node> nodes;
	nodes.resize(map.w() * map.h());

	indexer index(map.w(), map.h());
	comp node_comp(nodes);

	int xmin = loc.x, xmax = loc.x, ymin = loc.y, ymax = loc.y, nb_dest = 1;

	nodes[index(loc)] = node(move_left, turns_left, map_location::null_location, loc);
	std::vector<int> pq;
	pq.push_back(index(loc));

	while (!pq.empty()) {
		node& n = nodes[pq.front()];
		std::pop_heap(pq.begin(), pq.end(), node_comp);
		pq.pop_back();
		n.in = search_counter;

		std::set<map_location> allowed_teleports;
		teleports.get_adjacents(allowed_teleports, n.curr);
		std::vector<map_location> locs(6 + allowed_teleports.size());
		std::copy(allowed_teleports.begin(), allowed_teleports.end(), locs.begin() + 6);
		get_adjacent_tiles(n.curr, &locs[0]);
		for (int i = locs.size(); i-- > 0; ) {
			if (!locs[i].valid(map.w(), map.h())) continue;

			if (locs[i] == n.curr) continue;

			node& next = nodes[index(locs[i])];

			bool next_visited = next.in - search_counter <= 1u;

			// Classic Dijkstra allow to skip chosen nodes (with next.in==search_counter)
			// But the cost function and hex grid allow to also skip visited nodes:
			// if next was visited, then we already have a path 'src-..-n2-next'
			// - n2 was chosen before n, meaning that it is nearer to src.
			// - the cost of 'n-next' can't be smaller than 'n2-next' because
			//   cost is independent of direction and we don't have more MP at n
			//   (important because more MP may allow to avoid waiting next turn)
			// Thus, 'src-..-n-next' can't be shorter.
			if (next_visited) continue;

			const int move_cost = u.movement_cost(map[locs[i]]);

			node t = node(n.movement_left, n.turns_left, n.curr, locs[i]);
			if (t.movement_left < move_cost) {
				t.movement_left = total_movement;
				t.turns_left--;
			}

			if (t.movement_left < move_cost || t.turns_left < 0) continue;

			t.movement_left -= move_cost;

			if (!ignore_units) {
				const unit *v =
					get_visible_unit(locs[i], viewing_team, see_all);
				if (v && current_team.is_enemy(v->side()))
					continue;

				if (!force_ignore_zocs && t.movement_left > 0
				    && pathfind::enemy_zoc(teams, locs[i], viewing_team, u.side(), see_all)
						&& !u.get_ability_bool("skirmisher", locs[i])) {
					t.movement_left = 0;
				}
			}

			++nb_dest;
			int x = locs[i].x;
			if (x < xmin) xmin = x;
			if (xmax < x) xmax = x;
			int y = locs[i].y;
			if (y < ymin) ymin = y;
			if (ymax < y) ymax = y;

			bool in_list = next.in == search_counter + 1;
			t.in = search_counter + 1;
			next = t;

			// if already in the priority queue then we just update it, else push it.
			if (in_list) { // never happen see next_visited above
//.........这里部分代码省略.........
开发者ID:RushilPatel,项目名称:BattleForWesnoth,代码行数:101,代码来源:pathfind.cpp

示例8: un_recall_unit_cost

int un_recall_unit_cost(const unit& u)  // this really belongs elsewhere, perhaps in undo.cpp
{					// but I'm too lazy to do it at the moment
	stats& s = get_stats(get_team_save_id(u));
	s.recalls[u.type_id()]--;
	return u.recall_cost();
}
开发者ID:Martin9295,项目名称:wesnoth,代码行数:6,代码来源:statistics.cpp

示例9: advance_unit

void advance_unit(const unit& u)
{
	stats& s = get_stats(get_team_save_id(u));
	s.advanced_to[u.type_id()]++;
}
开发者ID:Martin9295,项目名称:wesnoth,代码行数:5,代码来源:statistics.cpp

示例10: un_recall_unit

void un_recall_unit(const unit& u)
{
	stats& s = get_stats(get_team_save_id(u));
	s.recalls[u.type_id()]--;
	s.recall_cost -= u.cost();
}
开发者ID:Martin9295,项目名称:wesnoth,代码行数:6,代码来源:statistics.cpp

示例11: un_recruit_unit

void un_recruit_unit(const unit& u)
{
	stats& s = get_stats(get_team_save_id(u));
	s.recruits[u.type().base_id()]--;
	s.recruit_cost -= u.cost();
}
开发者ID:Martin9295,项目名称:wesnoth,代码行数:6,代码来源:statistics.cpp

示例12: recall_unit

void recall_unit(const unit& u)
{
	stats& s = get_stats(get_team_save_id(u));
	s.recalls[u.type_id()]++;
	s.recall_cost += u.cost();
}
开发者ID:Martin9295,项目名称:wesnoth,代码行数:6,代码来源:statistics.cpp

示例13: human_team_can_ai

bool human_team_can_ai(const unit& u)
{
	return !u.human() || tent::tower_mode() || u.provoked_turns() || u.task() == unit::TASK_GUARD;
}
开发者ID:hyrio,项目名称:War-Of-Kingdom,代码行数:4,代码来源:playsingle_controller.cpp

示例14: choose_defender_weapon

int battle_context::choose_attacker_weapon(const unit& attacker,
		const unit& defender,
		const unit_map& units,
		const map_location& attacker_loc,
		const map_location& defender_loc,
		double harm_weight,
		int* defender_weapon,
		const combatant* prev_def)
{
	std::vector<unsigned int> choices;

	// What options does attacker have?
	unsigned int i;

	for(i = 0; i < attacker.attacks().size(); ++i) {
		const attack_type& att = attacker.attacks()[i];

		if(att.attack_weight() > 0) {
			choices.push_back(i);
		}
	}

	if(choices.empty()) {
		return -1;
	}

	if(choices.size() == 1) {
		*defender_weapon
				= choose_defender_weapon(attacker, defender, choices[0], units, attacker_loc, defender_loc, prev_def);
		const_attack_ptr def_weapon
				= *defender_weapon >= 0 ? defender.attacks()[*defender_weapon].shared_from_this() : nullptr;
		attacker_stats_.reset(new battle_context_unit_stats(
				attacker, attacker_loc, choices[0], true, defender, defender_loc, def_weapon, units));

		if(attacker_stats_->disable) {
			return -1;
		}

		const attack_type& att = attacker.attacks()[choices[0]];
		defender_stats_.reset(new battle_context_unit_stats(
				defender, defender_loc, *defender_weapon, false, attacker, attacker_loc, att.shared_from_this(), units));

		return choices[0];
	}

	// Multiple options: simulate them, save best.
	std::unique_ptr<battle_context_unit_stats> best_att_stats(nullptr);
	std::unique_ptr<battle_context_unit_stats> best_def_stats(nullptr);

	std::unique_ptr<combatant> best_att_comb(nullptr);
	std::unique_ptr<combatant> best_def_comb(nullptr);

	for(i = 0; i < choices.size(); ++i) {
		const attack_type& att = attacker.attacks()[choices[i]];

		int def_weapon =
			choose_defender_weapon(attacker, defender, choices[i], units, attacker_loc, defender_loc, prev_def);

		// If that didn't simulate, do so now.
		if(!attacker_combatant_) {
			const_attack_ptr def = nullptr;

			if(def_weapon >= 0) {
				def = defender.attacks()[def_weapon].shared_from_this();
			}

			attacker_stats_.reset(new battle_context_unit_stats(
					attacker, attacker_loc, choices[i], true, defender, defender_loc, def, units));

			if(attacker_stats_->disable) {
				continue;
			}

			defender_stats_.reset(new battle_context_unit_stats(
					defender, defender_loc, def_weapon, false, attacker, attacker_loc, att.shared_from_this(), units));

			attacker_combatant_.reset(new combatant(*attacker_stats_));
			defender_combatant_.reset(new combatant(*defender_stats_, prev_def));

			attacker_combatant_->fight(*defender_combatant_);
		} else {
			if(attacker_stats_ != nullptr && attacker_stats_->disable) {
				continue;
			}
		}

		if(!best_att_comb ||
			better_combat(*attacker_combatant_, *defender_combatant_, *best_att_comb, *best_def_comb, harm_weight)
		) {
			best_att_comb = std::move(attacker_combatant_);
			best_def_comb = std::move(defender_combatant_);
			best_att_stats = std::move(attacker_stats_);
			best_def_stats = std::move(defender_stats_);
		}

		attacker_combatant_.reset();
		defender_combatant_.reset();
		attacker_stats_.reset();
		defender_stats_.reset();
	}
//.........这里部分代码省略.........
开发者ID:fluffbeast,项目名称:wesnoth-old,代码行数:101,代码来源:attack.cpp

示例15: animate_unit_advancement

bool animate_unit_advancement(unit& u, size_t choice)
{
	const events::command_disabler cmd_disabler;

	if (u.advances() == false) {
		return false;
	}

	const std::vector<std::string>& options = u.advances_to();
	std::vector<config> mod_options = u.get_modification_advances();

	if (choice >= options.size() + mod_options.size()) {
		return false;
	}

	// When the unit advances, it fades to white, and then switches
	// to the new unit, then fades back to the normal colour

	game_display* disp = resources::screen;
	rect_of_hexes& draw_area = disp->draw_area();
	bool force_scroll = preferences::scroll_to_action();
	bool animate = force_scroll || point_in_rect_of_hexes(u.get_location().x, u.get_location().y, draw_area);
	if (!resources::screen->video().update_locked() && animate) {
		unit_animator animator;
		bool with_bars = true;
		animator.add_animation(&u, "levelout", u.get_location(), map_location(), 0, with_bars);
		animator.start_animations();
		animator.wait_for_end();
	}

	if(choice < options.size()) {
		// chosen_unit is not a reference, since the unit may disappear at any moment.
		std::string chosen_unit = options[choice];
		::advance_unit(u, chosen_unit);
	} else {
		unit* amla_unit = &u;
		const config &mod_option = mod_options[choice - options.size()];
		
		game_events::fire("advance", u.get_location());

		amla_unit->get_experience(increase_xp::attack_ublock(*amla_unit), -amla_unit->max_experience()); // subtract xp required
		// ALMA may want to change status, but add_modification in modify_according_to_hero cannot change state,
		// so it need call amla_unit->add_modification instead of amla_unit->modify_according_to_hero.
		amla_unit->add_modification(mod_option);

		game_events::fire("post_advance", u.get_location());
	}

	resources::screen->invalidate_unit();

	if (!resources::screen->video().update_locked()) {
		if (force_scroll || point_in_rect_of_hexes(u.get_location().x, u.get_location().y, draw_area)) {
			unit_animator animator;
			animator.add_animation(&u, "levelin", u.get_location(), map_location(), 0, true);
			animator.start_animations();
			animator.wait_for_end();
			animator.set_all_standing();
		
			resources::screen->invalidate(u.get_location());
			resources::screen->draw();
		}
		events::pump();
	}

	resources::screen->invalidate_all();
	if (force_scroll || point_in_rect_of_hexes(u.get_location().x, u.get_location().y, draw_area)) {
		resources::screen->draw();
	}

	return true;
}
开发者ID:hyrio,项目名称:War-Of-Kingdom,代码行数:71,代码来源:dialogs.cpp


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