Skip to content

Commit

Permalink
Decoupled small_profile from profile
Browse files Browse the repository at this point in the history
small_profile will not longer be generated from the value of profile. Instead,
it's being kept as an independent key intended for specifying a help image.
  • Loading branch information
Vultraz committed Mar 21, 2016
1 parent d2bec9e commit 05743bc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 50 deletions.
51 changes: 25 additions & 26 deletions src/units/types.cpp
Expand Up @@ -76,7 +76,7 @@ unit_type::unit_type(const unit_type& o) :
image_(o.image_),
icon_(o.icon_),
small_profile_(o.small_profile_),
big_profile_(o.big_profile_),
profile_(o.profile_),
flag_rgb_(o.flag_rgb_),
num_traits_(o.num_traits_),
variations_(o.variations_),
Expand Down Expand Up @@ -133,7 +133,7 @@ unit_type::unit_type(const config &cfg, const std::string & parent_id) :
image_(cfg_["image"].str()),
icon_(),
small_profile_(),
big_profile_(),
profile_(),
flag_rgb_(cfg_["flag_rgb"].str()),
num_traits_(0),
gender_types_(),
Expand Down Expand Up @@ -264,8 +264,8 @@ void unit_type::build_help_index(const movement_type_map &mv_types,
image_ = cfg_["image"].str();
icon_ = cfg_["image_icon"].str();
small_profile_ = cfg_["small_profile"].str();
big_profile_ = cfg_["profile"].str();
adjust_profile(small_profile_, big_profile_, image_);
profile_ = cfg_["profile"].str();
adjust_profile(profile_);

alignment_ = unit_type::ALIGNMENT::NEUTRAL;
alignment_.parse(cfg_["alignment"].str());
Expand Down Expand Up @@ -1292,30 +1292,29 @@ const unit_race *unit_type_data::find_race(const std::string &key) const
unit_type_data unit_types;


/* ** ** */
void adjust_profile(std::string& profile)
{
// Create a temp copy
std::string temp = profile;

static const std::string path_adjust = "/transparent";
const std::string::size_type offset = profile.find_last_of('/', profile.find('~'));

void adjust_profile(std::string &small, std::string &big, std::string const &def)
{
if (big.empty())
{
// No profile data; use the default image.
small = def;
big = def;
}
else if (small.empty())
{
// No small profile; use the current profile for it and
// try to infer the big one.
small = big;
std::string::size_type offset = big.find('~');
offset = big.find_last_of('/', offset);
if (offset != std::string::npos) {
big.insert(offset, "/transparent");
} else {
big = "transparent/" + big;
// If the path already refers to /transparent...
if(profile.find(path_adjust) != std::string::npos && offset != std::string::npos) {
if(!image::locator(profile).file_exists()) {
profile.replace(profile.find(path_adjust), path_adjust.length(), "");
}
if (!image::locator(big).file_exists())
big = small;

return;
}

// else, check for the file with /transparent appended...
offset != std::string::npos ?
temp.insert(offset, path_adjust) : temp = path_adjust + temp;

// and use that path if it exists
if(image::locator(temp).file_exists()) {
profile = temp;
}
}
6 changes: 3 additions & 3 deletions src/units/types.hpp
Expand Up @@ -128,7 +128,7 @@ class unit_type
const std::string& image() const { return image_; }
const std::string& icon() const { return icon_; }
const std::string &small_profile() const { return small_profile_; }
const std::string &big_profile() const { return big_profile_; }
const std::string &big_profile() const { return profile_; }
std::string halo() const { return cfg_["halo"]; }
std::string ellipse() const { return cfg_["ellipse"]; }
bool generate_name() const { return cfg_["generate_name"].to_bool(true); }
Expand Down Expand Up @@ -252,7 +252,7 @@ class unit_type
std::string image_;
std::string icon_;
std::string small_profile_;
std::string big_profile_;
std::string profile_;
std::string flag_rgb_;

unsigned int num_traits_;
Expand Down Expand Up @@ -345,7 +345,7 @@ class unit_type_data

extern unit_type_data unit_types;

void adjust_profile(std::string &small, std::string &big, std::string const &def);
void adjust_profile(std::string& profile);

struct unit_experience_accelerator {
unit_experience_accelerator(int modifier);
Expand Down
46 changes: 25 additions & 21 deletions src/units/unit.cpp
Expand Up @@ -152,7 +152,7 @@ namespace {
}
while(cur != end) {
WRN_UT << "Unknown attribute '" << cur->first << "' discarded." << std::endl;
++cur;
++cur;
}
}
}
Expand Down Expand Up @@ -493,11 +493,14 @@ unit::unit(const config &cfg, bool use_traits, const vconfig* vcfg, n_unit::id_m
}

if (const config::attribute_value *v = cfg.get("profile")) {
std::string big = *v, small = cfg["small_profile"];
adjust_profile(small, big, "");
profile_ = big;
small_profile_ = small;
std::string profile = (*v).str();
adjust_profile(profile);
profile_ = profile;
}
if (const config::attribute_value *v = cfg.get("small_profile")) {
small_profile_ = (*v).str();
}

max_hit_points_ = std::max(1, cfg["max_hitpoints"].to_int(max_hit_points_));
max_movement_ = std::max(0, cfg["max_moves"].to_int(max_movement_));
max_experience_ = std::max(1, cfg["max_experience"].to_int(max_experience_));
Expand Down Expand Up @@ -1392,7 +1395,7 @@ void unit::write(config& cfg) const
write_upkeep(cfg["upkeep"]);
cfg["hitpoints"] = hit_points_;
cfg["max_hitpoints"] = max_hit_points_;

cfg["image_icon"] = type().icon();
cfg["image"] = type().image();
cfg["random_traits"] = random_traits_;
Expand Down Expand Up @@ -1718,7 +1721,7 @@ std::string unit::describe_builtin_effect(std::string apply_to, const config& ef
if(apply_to == "attack") {
std::string attack_names;
bool first_attack = true;

std::string desc;
for(std::vector<attack_type>::iterator a = attacks_.begin();
a != attacks_.end(); ++a) {
Expand All @@ -1729,7 +1732,7 @@ std::string unit::describe_builtin_effect(std::string apply_to, const config& ef
} else {
attack_names += t_string(N_(" and "), "wesnoth");
}

attack_names += t_string(a->name(), "wesnoth-units");
}
}
Expand All @@ -1741,41 +1744,41 @@ std::string unit::describe_builtin_effect(std::string apply_to, const config& ef
}
} else if(apply_to == "hitpoints") {
const std::string &increase_total = effect["increase_total"];

if(!increase_total.empty()) {
return utils::print_modifier(increase_total) + " " +
t_string(N_("HP"), "wesnoth");
}
} else if(apply_to == "movement") {
const std::string &increase = effect["increase"];

if(!increase.empty()) {
int n = lexical_cast<int>(increase);
return utils::print_modifier(increase) + " " +
_n("move", "moves", n);
}
} else if(apply_to == "vision") {
const std::string &increase = effect["increase"];

if(!increase.empty()) {
return utils::print_modifier(increase) + " " + t_string(N_("vision"), "wesnoth");
}
} else if(apply_to == "jamming") {
const std::string &increase = effect["increase"];

if(!increase.empty()) {
return utils::print_modifier(increase) + " " + t_string(N_("jamming"), "wesnoth");
}
} else if(apply_to == "max_experience") {
const std::string &increase = effect["increase"];

if(!increase.empty()) {
return utils::print_modifier(increase) + " " +
t_string(N_("XP to advance"), "wesnoth");
}
} else if (apply_to == "max_attacks") {
const std::string &increase = effect["increase"];

std::string description = utils::print_modifier(increase) + " ";
const char* const singular = N_("attack per turn");
const char* const plural = N_("attacks per turn");
Expand Down Expand Up @@ -1805,11 +1808,12 @@ void unit::apply_builtin_effect(std::string apply_to, const config& effect)
}
else if(apply_to == "profile") {
if (const config::attribute_value *v = effect.get("portrait")) {
std::string big = *v, small = effect["small_portrait"];
adjust_profile(small, big, "");

profile_ = big;
small_profile_ = small;
std::string portrait = (*v).str();
adjust_profile(portrait);
profile_ = portrait;
}
if (const config::attribute_value *v = effect.get("small_portrait")) {
small_profile_ = (*v).str();
}
if (const config::attribute_value *v = effect.get("description")) {
description_ = *v;
Expand Down Expand Up @@ -2258,7 +2262,7 @@ void unit::add_trait_description(const config& trait, const t_string& descriptio
}

std::string unit::absolute_image() const {

return type().icon().empty() ? type().image() : type().icon();
}

Expand Down Expand Up @@ -2475,7 +2479,7 @@ void unit::parse_upkeep(const config::attribute_value& upkeep)
if (upkeep.empty()) {
return;
}
//TODO: create abetter way to check whether it is actually an int.
//TODO: create abetter way to check whether it is actually an int.
int upkeep_int = upkeep.to_int(-99);
if(upkeep_int != -99) {
upkeep_ = upkeep_int;
Expand Down

0 comments on commit 05743bc

Please sign in to comment.