From 684d1c35c64e9317e331f3860aeb0fb98088ac28 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Sun, 25 May 2014 14:38:00 -0400 Subject: [PATCH] unit_type::alignment to use MAKE_ENUM macro --- src/callable_objects.cpp | 2 +- src/reports.cpp | 2 +- src/unit_types.cpp | 28 ++++------------------------ src/unit_types.hpp | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/callable_objects.cpp b/src/callable_objects.cpp index 7c52e1db35ca..e35df1a1ca8c 100644 --- a/src/callable_objects.cpp +++ b/src/callable_objects.cpp @@ -280,7 +280,7 @@ variant unit_type_callable::get_value(const std::string& key) const } else if(key == "type") { return variant(u_.type_name()); } else if(key == "alignment") { - return variant(u_.alignment_id(u_.alignment())); + return variant(lexical_cast(u_.alignment())); } else if(key == "abilities") { std::vector abilities = u_.get_ability_list(); std::vector res; diff --git a/src/reports.cpp b/src/reports.cpp index b1a7261658b8..e0b8c017b44a 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -348,7 +348,7 @@ static config unit_alignment(const unit* u) if (!u) return report(); std::ostringstream str, tooltip; char const *align = unit_type::alignment_description(u->alignment(), u->gender()); - std::string align_id = unit_type::alignment_id(u->alignment()); + std::string align_id = lexical_cast(u->alignment()); int cm = combat_modifier(resources::screen->displayed_unit_hex(), u->alignment(), u->is_fearless()); diff --git a/src/unit_types.cpp b/src/unit_types.cpp index 8f71cd2d6d51..08236de3f213 100644 --- a/src/unit_types.cpp +++ b/src/unit_types.cpp @@ -22,7 +22,7 @@ #include "unit_types.hpp" #include "game_config.hpp" -#include "gettext.hpp" +//#include "gettext.hpp" #include "loadscreen.hpp" #include "log.hpp" #include "portrait.hpp" @@ -486,21 +486,7 @@ void unit_type::build_full(const movement_type_map &mv_types, gender_types_[i]->build_full(mv_types, races, traits); } - const std::string& align = cfg_["alignment"]; - if(align == "lawful") - alignment_ = LAWFUL; - else if(align == "chaotic") - alignment_ = CHAOTIC; - else if(align == "neutral") - alignment_ = NEUTRAL; - else if(align == "liminal") - alignment_ = LIMINAL; - else { - if ( !align.empty() ) { - ERR_CF << "Invalid alignment found for " << log_id() << ": '" << align << "'" << std::endl; - } - alignment_ = NEUTRAL; - } + alignment_ = lexical_cast_default(cfg_["alignment"], unit_type::NEUTRAL); if ( race_ != &unit_race::null_race ) { @@ -835,7 +821,7 @@ int unit_type::experience_needed(bool with_acceleration) const } return experience_needed_; } - +/* const char* unit_type::alignment_description(unit_type::ALIGNMENT align, unit_race::GENDER gender) { static const char* aligns[] = { N_("lawful"), N_("neutral"), N_("chaotic"), N_("liminal") }; @@ -843,13 +829,7 @@ const char* unit_type::alignment_description(unit_type::ALIGNMENT align, unit_ra const char** tlist = (gender == unit_race::MALE ? aligns : aligns_female); return (sgettext(tlist[align])); -} - -const char* unit_type::alignment_id(unit_type::ALIGNMENT align) -{ - static const char* aligns[] = { "lawful", "neutral", "chaotic", "liminal" }; - return (aligns[align]); -} +}*/ bool unit_type::has_ability_by_id(const std::string& ability) const { diff --git a/src/unit_types.hpp b/src/unit_types.hpp index 975c8c9c5426..46452d43f2e1 100644 --- a/src/unit_types.hpp +++ b/src/unit_types.hpp @@ -14,12 +14,18 @@ #ifndef UNIT_TYPES_H_INCLUDED #define UNIT_TYPES_H_INCLUDED +#include "gettext.hpp" +#include "make_enum.hpp" #include "map_location.hpp" #include "movetype.hpp" #include "race.hpp" #include "util.hpp" #include +#include +#include +#include +#include struct tportrait; class unit_ability_list; @@ -222,11 +228,32 @@ class unit_type int old_value_; }; - enum ALIGNMENT { LAWFUL, NEUTRAL, CHAOTIC, LIMINAL }; + //enum ALIGNMENT { LAWFUL, NEUTRAL, CHAOTIC, LIMINAL }; + MAKE_ENUM (ALIGNMENT, + (LAWFUL, N_("lawful")) + (NEUTRAL, N_("neutral")) + (CHAOTIC, N_("chaotic")) + (LIMINAL, N_("liminal")) + ) + MAKE_ENUM (ALIGNMENT_FEMALE_VARIATION, + (FEMALE_LAWFUL, N_("female^lawful")) + (FEMALE_NEUTRAL, N_("female^neutral")) + (FEMALE_CHAOTIC, N_("female^chaotic")) + (FEMALE_LIMINAL, N_("female^liminal")) + ) ALIGNMENT alignment() const { return alignment_; } - static const char* alignment_description(ALIGNMENT align, unit_race::GENDER gender = unit_race::MALE); - static const char* alignment_id(ALIGNMENT align); + inline static const char* alignment_description(ALIGNMENT align, unit_race::GENDER gender = unit_race::MALE) + { + std::string str = std::string(); + if (gender == unit_race::FEMALE) { + ALIGNMENT_FEMALE_VARIATION fem = static_cast (align); + str = lexical_cast(fem); + } else { + str = lexical_cast(align); + } + return sgettext(str.c_str()); + } fixed_t alpha() const { return alpha_; } @@ -357,6 +384,9 @@ class unit_type std::vector portraits_; }; +MAKE_ENUM_STREAM_OPS2(unit_type, ALIGNMENT) +MAKE_ENUM_STREAM_OPS2(unit_type, ALIGNMENT_FEMALE_VARIATION) + class unit_type_data : private boost::noncopyable {