Commits

David Stone committed 2d56682

Improved Pokemon string conversions.

Comments (0)

Files changed (7)

source/clients/battle.cpp

 
 #include "../pokemon/pokemon_not_found.hpp"
 
+#include "../string_conversions/conversion.hpp"
+
 #include "../team_predictor/team_predictor.hpp"
 
 namespace technicalmachine {
 	// replacing fainted Pokemon).
 	auto const replacement = switcher.all_pokemon().replacement();
 	if (switcher.is_me())
-		std::cerr << switcher.pokemon(replacement).to_string() << '\n';
+		std::cerr << to_string(switcher.pokemon(replacement).name()) << '\n';
 	
 	// This assumes Species Clause is in effect
 	bool const added = switcher.all_pokemon().add_if_not_present(species, level, gender, nickname);
 	Team const & team = get_team(damaged);
 	auto const & pokemon = team.replacement();
 	std::cerr << "is me: " << team.is_me() << '\n';
-	std::cerr << pokemon.to_string() << '\n';
+	std::cerr << to_string(pokemon.name()) << '\n';
 	assert(move_damage);
 	Rational const change(visible_damage, max_visible_hp_change(team));
 	auto const damage = pokemon.stat(Stat::HP).max * change;
 		unsigned const max_value = tm_estimate + 1;
 		assert(max_value > tm_estimate);
 		if (!(min_value <= new_hp and new_hp <= max_value)) {
-			std::cerr << "Uh oh! " + pokemon.to_string() + " has the wrong HP! The server reports ";
+			std::cerr << "Uh oh! " + to_string(pokemon.name()) + " has the wrong HP! The server reports ";
 			if (!team.is_me())
 				std::cerr << "approximately ";
 			std::cerr << reported_hp << " HP remaining, but TM thinks it has " << pokemon.stat(Stat::HP).stat << ".\n";
 	// "slot" is only useful in situations other than 1v1, which TM does not yet
 	// support.
 	auto const team = get_team(fainter);
-	std::cerr << team.pokemon().to_string() << " fainted\n";
+	std::cerr << to_string(team.pokemon().name()) << " fainted\n";
 	updated_hp.faint(team.is_me(), team.pokemon());
 }
 
 		replacement(*last, *first);
 	}
 	else {
-		std::cout << "First move: " + first->pokemon().to_string() + " uses " + first->pokemon().move().to_string() + '\n';
-		std::cout << "Last move: " + last->pokemon().to_string() + " uses " + last->pokemon().move().to_string() + '\n';
+		std::cout << "First move: " + to_string(first->pokemon().name()) + " uses " + first->pokemon().move().to_string() + '\n';
+		std::cout << "Last move: " + to_string(last->pokemon().name()) + " uses " + last->pokemon().move().to_string() + '\n';
 		// Anything with recoil will mess this up
 		
 		constexpr bool damage_is_known = true;

source/evaluate/expectiminimax.cpp

 	auto const ai_break_out = [& ai]() { return ai.pokemon().stat(Stat::HP).stat != 0; };
 	ai.all_pokemon().for_each_replacement(ai_break_out, [&]() {
 		if (verbose or first_turn)
-			std::cout << std::string(tabs, '\t') + "Evaluating switching to " + ai.all_pokemon().at_replacement().to_string() + "\n";
+			std::cout << std::string(tabs, '\t') + "Evaluating switching to " + to_string(ai.all_pokemon().at_replacement().name()) + "\n";
 		int64_t beta = Evaluate::victory + 1;
 		auto const foe_break_out = [& foe, & alpha, & beta]() {
 			return beta <= alpha or foe.pokemon().stat(Stat::HP).stat != 0;
 	}
 	switcher.all_pokemon().for_each_replacement([&]() {
 		if (first_turn)
-			std::cout << std::string (tabs, '\t') + "Evaluating bringing in " + switcher.all_pokemon().at_replacement ().to_string () + "\n";
+			std::cout << std::string (tabs, '\t') + "Evaluating bringing in " + to_string(switcher.all_pokemon().at_replacement().name()) + "\n";
 		int64_t const value = switch_after_move_branch(switcher, other, switcher_variable, other_variable, weather, depth, evaluate);
 		if (switcher.is_me())
 			update_best_move (alpha, value, first_turn, switcher.all_pokemon().replacement_to_switch(), best_switch);
 
 void print_best_move (Team const & team, Moves const best_move, int64_t score) {
 	if (Move::is_switch (best_move))
-		std::cout << "Switch to " + team.pokemon(Move::to_replacement(best_move)).to_string();
+		std::cout << "Switch to " + to_string(team.pokemon(Move::to_replacement(best_move)).name());
 	else
 		std::cout << "Use " + to_string(best_move);
 	std::cout << " for a minimum expected score of " << score << "\n";
 		if (!team.is_me())
 			++tabs;
 		std::cout << std::string (tabs, '\t') + "Evaluating ";
-		if (team.pokemon().move().is_switch())
-			std::cout << "switching to " + team.pokemon(team.pokemon().move().to_replacement()).to_string () + "\n";
-		else
+		if (team.pokemon().move().is_switch()) {
+			auto const replacement_index = team.pokemon().move().to_replacement();
+			std::cout << "switching to " + to_string(team.pokemon(replacement_index).name()) + "\n";
+		}
+		else {
 			std::cout << team.pokemon().move().to_string() + "\n";
+		}
 	}
 }
 

source/pokemon/active_pokemon.cpp

 	return !(lhs == rhs);
 }
 
-std::string ActivePokemon::to_string() const {
-	return get_pokemon().to_string();
-}
-
 }	// namespace technicalmachine

source/pokemon/active_pokemon.hpp

 	typedef uint64_t hash_type;
 	hash_type hash() const;
 	hash_type max_hash() const;
-	
-	std::string to_string() const;
 
 private:
 	Pokemon const & get_pokemon() const;

source/pokemon/pokemon.cpp

 #include <cstdint>
 #include <string>
 
+#include <boost/format.hpp>
+
 #include "species.hpp"
 
 #include "../ability.hpp"
 	#if defined TECHNICALMACHINE_POKEMON_USE_NICKNAMES
 		return nickname;
 	#else
-		return to_string();
+		return to_string(name());
 	#endif
 }
 
 
 }	// unnamed namespace
 
+std::string to_string(Pokemon const & pokemon, bool const include_nickname) {
+	std::string output = to_string(pokemon.name());
+	double const d_per_cent_hp = 100.0 * pokemon.current_hp();
+	std::string const per_cent_hp = str(boost::format("%.1f") % d_per_cent_hp);
+	output += " (" + per_cent_hp + "% HP)";
+	output += " @ " + pokemon.item().to_string();
+	if (include_nickname and pokemon.get_nickname() != to_string(pokemon.name())) {
+		output += " ** " + pokemon.get_nickname();
+	}
+	output += '\n';
+	if (pokemon.ability().is_set()) {
+		output += "\tAbility: " + pokemon.ability().to_string() + '\n';
+	}
+	if (!pokemon.status().is_clear()) {
+		output += "\tStatus: " + pokemon.status().to_string() + '\n';
+	}
+	output += "\tNature: " + pokemon.nature().to_string() + '\n';
+	output += "\t";
+	auto const add_stat = [&](Stat const & stat, std::string const & stat_name) {
+		if (stat_name != "HP") {
+			output += " / ";
+		}
+		output += std::to_string(stat.ev.value()) + " " + stat_name;
+	};
+	static std::pair<Stat::Stats, std::string> const stats [] = {
+		{ Stat::HP, "HP" },
+		{ Stat::ATK, "Atk" },
+		{ Stat::DEF, "Def" },
+		{ Stat::SPA, "SpA" },
+		{ Stat::SPD, "SpD" },
+		{ Stat::SPE, "Spe" },
+	};
+	for (auto const stat : stats) {
+		add_stat(pokemon.stat(stat.first), stat.second);
+	}
+	pokemon.move.for_each_regular_move([& output](Move const & move) {
+		output += "\n\t- " + move.to_string();
+	});
+	return output;
+}
+
 Type::Types Pokemon::calculate_hidden_power_type() const {
 	static constexpr std::pair<Stat::Stats, unsigned> modifiers[] = {
 		{ Stat::HP, 0 },
 	return mass_array[static_cast<unsigned>(name())];
 }
 
-std::string Pokemon::to_string() const {
-	return ::technicalmachine::to_string(name());
-}
-
 }	// namespace technicalmachine

source/pokemon/pokemon.hpp

 	unsigned apply_damage(unsigned damage);
 	void apply_healing(unsigned amount);
 	unsigned power_of_mass_based_moves() const;
-	std::string to_string () const;
 	std::string get_nickname () const;
 	bool can_confuse_with_chatter() const;
 	bool can_use_substitute() const;
 bool operator!= (Pokemon const & lhs, Pokemon const & rhs);
 
 bool is_alternate_form(Species first, Species second);
+std::string to_string(Pokemon const & pokemon, bool include_nickname = false);
 
 }	// namespace technicalmachine
 #endif	// POKEMON__POKEMON_HPP_
 
 #include <boost/algorithm/string.hpp>
 #include <boost/filesystem.hpp>
-#include <boost/format.hpp>
-#include <boost/lexical_cast.hpp>
 
 #include "ability.hpp"
 #include "damage.hpp"
 		output += team.who() + "'s team:\n";
 	}
 	for (auto const & member : team.all_pokemon()) {
-		output += member.to_string();
-		double const d_per_cent_hp = 100.0 * member.current_hp();
-		std::string const per_cent_hp = boost::lexical_cast <std::string> (boost::format ("%.1f") % d_per_cent_hp);
-		output += " (" + per_cent_hp + "% HP)";
-		output += " @ " + member.item().to_string ();
-		output += include_owner ? (" ** " + member.get_nickname() + '\n') : "\n";
-		if (member.ability().is_set()) {
-			output += "\tAbility: " + member.ability().to_string() + '\n';
-		}
-		if (!member.status().is_clear()) {
-			output += "\tStatus: " + member.status().to_string() + '\n';
-		}
-		output += "\tNature: " + member.nature().to_string() + '\n';
-		output += "\t";
-		auto const add_stat = [&](Stat const & stat, std::string const & stat_name) {
-			if (stat_name != "HP") {
-				output += " / ";
-			}
-			output += std::to_string(stat.ev.value()) + " " + stat_name;
-		};
-		static std::pair<Stat::Stats, std::string> const stats [] = {
-			{ Stat::HP, "HP" },
-			{ Stat::ATK, "Atk" },
-			{ Stat::DEF, "Def" },
-			{ Stat::SPA, "SpA" },
-			{ Stat::SPD, "SpD" },
-			{ Stat::SPE, "Spe" },
-		};
-		for (auto const stat : stats) {
-			add_stat(member.stat(stat.first), stat.second);
-		}
-		member.move.for_each_regular_move([& output](Move const & move) {
-			output += "\n\t- " + move.to_string();
-		});
+		output += to_string(member, include_owner) + '\n';
 	}
 	return output;
 }