Skip to content

Commit

Permalink
unit_type::alignment to use MAKE_ENUM macro
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed May 25, 2014
1 parent 7b87619 commit 684d1c3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/callable_objects.cpp
Expand Up @@ -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<std::string>(u_.alignment()));
} else if(key == "abilities") {
std::vector<std::string> abilities = u_.get_ability_list();
std::vector<variant> res;
Expand Down
2 changes: 1 addition & 1 deletion src/reports.cpp
Expand Up @@ -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<std::string>(u->alignment());
int cm = combat_modifier(resources::screen->displayed_unit_hex(), u->alignment(),
u->is_fearless());

Expand Down
28 changes: 4 additions & 24 deletions src/unit_types.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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<unit_type::ALIGNMENT>(cfg_["alignment"], unit_type::NEUTRAL);

if ( race_ != &unit_race::null_race )
{
Expand Down Expand Up @@ -835,21 +821,15 @@ 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") };
static const char* aligns_female[] = { N_("female^lawful"), N_("female^neutral"), N_("female^chaotic"), N_("female^liminal") };
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
{
Expand Down
36 changes: 33 additions & 3 deletions src/unit_types.hpp
Expand Up @@ -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 <boost/noncopyable.hpp>
#include <map>
#include <set>
#include <string>
#include <vector>

struct tportrait;
class unit_ability_list;
Expand Down Expand Up @@ -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<ALIGNMENT_FEMALE_VARIATION> (align);
str = lexical_cast<std::string>(fem);
} else {
str = lexical_cast<std::string>(align);
}
return sgettext(str.c_str());
}

fixed_t alpha() const { return alpha_; }

Expand Down Expand Up @@ -357,6 +384,9 @@ class unit_type
std::vector<tportrait> portraits_;
};

MAKE_ENUM_STREAM_OPS2(unit_type, ALIGNMENT)
MAKE_ENUM_STREAM_OPS2(unit_type, ALIGNMENT_FEMALE_VARIATION)

class unit_type_data
: private boost::noncopyable
{
Expand Down

0 comments on commit 684d1c3

Please sign in to comment.