Keep track of the original unit type ID (variations can override it).

Use the original (base) ID when creating units.
This commit is contained in:
J. Tyne 2013-01-20 02:43:38 +00:00
parent bdffe47578
commit d66a950a31
9 changed files with 30 additions and 17 deletions

View file

@ -125,7 +125,7 @@ battle_context_unit_stats::battle_context_unit_stats(const unit &u,
if (plagues) {
plague_type = (*plague_specials.front().first)["type"].str();
if (plague_type.empty())
plague_type = u.type_id();
plague_type = u.type().base_id();
}
// Compute chance to hit.

View file

@ -512,7 +512,7 @@ void undo_list::redo()
// Redo recruit action
map_location loc = action.route.front();
map_location from = action.recall_from;
const std::string name = action.affected_unit->type_id();
const std::string name = action.affected_unit->type().base_id();
//search for the unit to be recruited in recruits
int recruit_num = 0;
@ -531,7 +531,7 @@ void undo_list::redo()
}
current_team.last_recruit(name);
recorder.add_recruit(recruit_num,loc,from);
const std::string &msg = find_recruit_location(side_, loc, from, action.affected_unit->type_id());
const std::string &msg = find_recruit_location(side_, loc, from, action.affected_unit->type().base_id());
if(msg.empty()) {
const unit new_unit = *action.affected_unit;
//unit new_unit(action.affected_unit->type(),team_num_,true);

View file

@ -216,7 +216,7 @@ bool ai_default_recruitment_stage::recruit_usage(const std::string& usage)
if (imc != maximum_counts_.end()) {
int count_active = 0;
for (unit_map::const_iterator u = resources::units->begin(); u != resources::units->end(); ++u) {
if (u->side() == get_side() && !u->incapacitated() && u->type_id() == name) {
if (u->side() == get_side() && !u->incapacitated() && u->type().base_id() == name) {
++count_active;
}
}
@ -438,7 +438,7 @@ int ai_default_recruitment_stage::get_combat_score(const unit_type& ut) const
continue;
}
get_combat_score_vs(ut, j->type_id(), score, weighting, j->hitpoints(), j->max_hitpoints());
get_combat_score_vs(ut, j->type().base_id(), score, weighting, j->hitpoints(), j->max_hitpoints());
}
if(weighting != 0) {

View file

@ -207,7 +207,7 @@ class fake_team
{
int counter = 0;
BOOST_FOREACH(unit &un, *resources::units){
if(un.side() == side() && un.type_id() == name) // @todo: is type_id good?
if(un.side() == side() && un.type().base_id() == name) // @todo: is base_id good?
{
counter++;
}
@ -433,7 +433,7 @@ static std::vector<potential_recruit> ai_choose_best_recruits(fake_team &t, int
{
if (i.side()==t.side())
{
current_units[(i.type_id())]++;
current_units[i.type().base_id()]++;
}
}
int gold = t.gold();

View file

@ -474,7 +474,7 @@ void attack_context::defend_result(hit_result res, int damage, int drain)
void recruit_unit(const unit& u)
{
stats& s = get_stats(u.side_id());
s.recruits[u.type_id()]++;
s.recruits[u.type().base_id()]++;
s.recruit_cost += u.cost();
}
@ -495,7 +495,7 @@ void un_recall_unit(const unit& u)
void un_recruit_unit(const unit& u)
{
stats& s = get_stats(u.side_id());
s.recruits[u.type_id()]--;
s.recruits[u.type().base_id()]--;
s.recruit_cost -= u.cost();
}

View file

@ -203,7 +203,7 @@ unit::unit(const config &cfg, bool use_traits, game_state* state, const vconfig*
cfg_(),
loc_(cfg["x"] - 1, cfg["y"] - 1),
advances_to_(),
type_(&get_unit_type(cfg["type"])),
type_(&get_unit_type(cfg["parent_type"].blank() ? cfg["type"] : cfg["parent_type"])),
type_name_(),
race_(&unit_race::null_race),
id_(cfg["id"]),
@ -542,6 +542,7 @@ unit::unit(const config &cfg, bool use_traits, game_state* state, const vconfig*
"advances_to", "hitpoints", "goto_x", "goto_y", "moves",
"experience", "resting", "unrenamable", "alignment",
"canrecruit", "extra_recruit", "x", "y", "placement",
"parent_type",
// Useless attributes created when saving units to WML:
"flag_rgb", "language_name" };
BOOST_FOREACH(const char *attr, internalized_attrs) {
@ -726,7 +727,7 @@ void unit::generate_name(rand_rng::simple_rng* rng)
void unit::generate_traits(bool musthaveonly)
{
LOG_UT << "Generating a trait for unit type " << type_id() << " with musthaveonly " << musthaveonly << "\n";
LOG_UT << "Generating a trait for unit type " << type().log_id() << " with musthaveonly " << musthaveonly << "\n";
const unit_type &u_type = type();
// Calculate the unit's traits
@ -1047,12 +1048,12 @@ const std::vector<std::string> unit::advances_to_translated() const
std::vector<std::string> result;
BOOST_FOREACH(std::string adv_type_id, advances_to_)
{
const unit_type *type = unit_types.find(adv_type_id);
if (type)
result.push_back(type->type_name());
const unit_type *adv_type = unit_types.find(adv_type_id);
if ( adv_type )
result.push_back(adv_type->type_name());
else
WRN_UT << "unknown unit in advances_to list of type "
<< type_id() << ": " << adv_type_id << "\n";
<< type().log_id() << ": " << adv_type_id << "\n";
}
return result;
}
@ -1690,6 +1691,8 @@ void unit::write(config& cfg) const
cfg["side"] = side_;
cfg["type"] = type_id();
if ( type_id() != type().base_id() )
cfg["parent_type"] = type().base_id();
//support for unit formulas in [ai] and unit-specific variables in [ai] [vars]
@ -2824,7 +2827,7 @@ void unit::add_modification(const std::string& mod_type, const config& mod, bool
advance_to(type());
} else if ((last_effect)["apply_to"] == "type") {
config::attribute_value &prev_type = (*new_child)["prev_type"];
if (prev_type.blank()) prev_type = type_id();
if (prev_type.blank()) prev_type = type().base_id();
const std::string& new_type_id = last_effect["name"];
const unit_type* new_type = unit_types.find(new_type_id);
if ( new_type ) {

View file

@ -102,7 +102,11 @@ public:
const std::vector<std::string> advances_to_translated() const;
void set_advances_to(const std::vector<std::string>& advances_to);
/** The type id of the unit */
/**
* The id of the type of the unit.
* If you are dealing with creating units (e.g. recruitment), this is not what
* you want, as a variation can change this; use type().base_id() instead.
*/
const std::string& type_id() const { return type_->id(); }
/** The type of the unit (accounting for gender and variation). */
const unit_type& type() const { return *type_; }

View file

@ -604,6 +604,7 @@ unit_type::unit_type(const unit_type& o) :
cfg_(o.cfg_),
id_(o.id_),
debug_id_(o.debug_id_),
base_id_(o.base_id_),
type_name_(o.type_name_),
description_(o.description_),
hitpoints_(o.hitpoints_),
@ -654,6 +655,7 @@ unit_type::unit_type(const config &cfg, const std::string & parent_id) :
cfg_(cfg),
id_(cfg_.has_attribute("id") ? cfg_["id"].str() : parent_id),
debug_id_(),
base_id_(!parent_id.empty() ? parent_id : id_),
type_name_(cfg_["name"].t_str()),
description_(),
hitpoints_(0),
@ -887,6 +889,7 @@ void unit_type::build_help_index(const movement_type_map &mv_types,
unit_type *ut = new unit_type(var_cfg, id_);
ut->debug_id_ = debug_id_ + " [" + var_name + "]";
ut->base_id_ = base_id_; // In case this is not id_.
ut->build_help_index(mv_types, races, traits);
variations_.insert(std::make_pair(var_name, ut));
}

View file

@ -248,6 +248,8 @@ public:
const std::string& id() const { return id_; }
/// A variant on id() that is more descriptive, for use with message logging.
const std::string log_id() const { return id_ + debug_id_; }
/// The id of the original type from which this (variation) descended.
const std::string& base_id() const { return base_id_; }
// NOTE: this used to be a const object reference, but it messed up with the
// translation engine upon changing the language in the same session.
t_string unit_description() const;
@ -332,6 +334,7 @@ private:
std::string id_;
std::string debug_id_; /// A suffix for id_, used when logging messages.
std::string base_id_; /// The id of the top ancestor of this unit_type.
t_string type_name_;
t_string description_;
int hitpoints_;