Push the logic controlling incremental building of unit types...

...into the unit_type class.
This commit is contained in:
J. Tyne 2013-01-03 21:51:06 +00:00
parent aab7c8afcf
commit c5564c1bd1
2 changed files with 70 additions and 32 deletions

View file

@ -714,6 +714,9 @@ unit_type::~unit_type()
}
}
/**
* Load data into an empty unit_type (build to FULL).
*/
void unit_type::build_full(const movement_type_map &mv_types,
const race_map &races, const config::const_child_itors &traits)
{
@ -797,6 +800,9 @@ void unit_type::build_full(const movement_type_map &mv_types,
build_status_ = FULL;
}
/**
* Partially load data into an empty unit_type (build to HELP_INDEX).
*/
void unit_type::build_help_index(const movement_type_map &mv_types,
const race_map &races, const config::const_child_itors &traits)
{
@ -900,6 +906,11 @@ void unit_type::build_help_index(const movement_type_map &mv_types,
build_status_ = HELP_INDEX;
}
/**
* Load the most needed data into an empty unit_type (build to CREATE).
* This creates the gender-specific types (if needed) and also defines how much
* experience is needed to advance as well as what this advances to.
*/
void unit_type::build_created(const movement_type_map &mv_types,
const race_map &races, const config::const_child_itors &traits)
{
@ -947,6 +958,39 @@ void unit_type::build_created(const movement_type_map &mv_types,
build_status_ = CREATED;
}
/**
* Performs a build of this to the indicated stage.
*/
void unit_type::build(BUILD_STATUS status, const movement_type_map &movement_types,
const race_map &races, const config::const_child_itors &traits)
{
DBG_UT << "Building unit type " << id_ << ", level " << status << '\n';
// Nothing to do if we are already built.
if ( int(status) <= int(build_status_) )
return;
switch (status) {
case CREATED:
// Build the basic data.
build_created(movement_types, races, traits);
return;
case HELP_INDEX:
// Build the data needed to feed the help index.
build_help_index(movement_types, races, traits);
return;
case WITHOUT_ANIMATIONS:
// Animations are now built when they are accessed, so fall down to FULL.
case FULL:
default:
build_full(movement_types, races, traits);
return;
}
}
const unit_type& unit_type::get_gender_unit_type(std::string gender) const
{
if (gender == unit_race::s_female) return get_gender_unit_type(unit_race::FEMALE);
@ -1249,6 +1293,9 @@ void unit_type_data::set_config(config &cfg)
}
}
/**
* Finds a unit_type by its id() and makes sure it is built to the specified level.
*/
const unit_type *unit_type_data::find(const std::string& key, unit_type::BUILD_STATUS status) const
{
if (key.empty() || key == "random") return NULL;
@ -1266,7 +1313,7 @@ const unit_type *unit_type_data::find(const std::string& key, unit_type::BUILD_S
}
//check if the unit_type is constructed and build it if necessary
build_unit_type(itor, status);
build_unit_type(itor->second, status);
return &itor->second;
}
@ -1309,7 +1356,7 @@ void unit_type_data::build_all(unit_type::BUILD_STATUS status)
assert(unit_cfg_ != NULL);
for (unit_type_map::iterator u = types_.begin(), u_end = types_.end(); u != u_end; ++u) {
build_unit_type(u, status);
build_unit_type(u->second, status);
loadscreen::increment_progress();
}
for (unit_type_map::iterator u = types_.begin(), u_end = types_.end(); u != u_end; ++u) {
@ -1319,28 +1366,6 @@ void unit_type_data::build_all(unit_type::BUILD_STATUS status)
build_status_ = status;
}
unit_type &unit_type_data::build_unit_type(const unit_type_map::iterator &ut, unit_type::BUILD_STATUS status) const
{
DBG_UT << "Building unit type " << ut->first << ", level " << status << '\n';
if (int(status) <= int(ut->second.build_status()))
return ut->second;
switch (status) {
case unit_type::CREATED:
ut->second.build_created(movement_types_, races_, unit_cfg_->child_range("trait"));
break;
case unit_type::HELP_INDEX:
// Build the data needed to feed the help index.
ut->second.build_help_index(movement_types_, races_, unit_cfg_->child_range("trait"));
break;
default:
ut->second.build_full(movement_types_, races_, unit_cfg_->child_range("trait"));
}
return ut->second;
}
void unit_type_data::read_hide_help(const config& cfg)
{
if (!cfg)

View file

@ -194,15 +194,28 @@ public:
~unit_type();
/** Load data into an empty unit_type */
/// Records the status of the lazy building of unit types.
enum BUILD_STATUS {NOT_BUILT, CREATED, HELP_INDEX, WITHOUT_ANIMATIONS, FULL};
private: // These will be called by build().
/// Load data into an empty unit_type (build to FULL).
void build_full(const movement_type_map &movement_types,
const race_map &races, const config::const_child_itors &traits);
/** Partially load data into an empty unit_type */
/// Partially load data into an empty unit_type (build to HELP_INDEX).
void build_help_index(const movement_type_map &movement_types,
const race_map &races, const config::const_child_itors &traits);
/** Load the most needed data into an empty unit_type */
/// Load the most needed data into an empty unit_type (build to CREATE).
void build_created(const movement_type_map &movement_types,
const race_map &races, const config::const_child_itors &traits);
public:
/// Performs a build of this to the indicated stage.
void build(BUILD_STATUS status, const movement_type_map &movement_types,
const race_map &races, const config::const_child_itors &traits);
/// Performs a build of this to the indicated stage.
/// (This does not logically change the unit type, so allow const access.)
void build(BUILD_STATUS status, const movement_type_map &movement_types,
const race_map &races, const config::const_child_itors &traits) const
{ const_cast<unit_type *>(this)->build(status, movement_types, races, traits); }
/**
* Adds an additional advancement path to a unit type.
@ -305,10 +318,6 @@ public:
const unit_race* race() const { return race_; }
bool hide_help() const;
enum BUILD_STATUS {NOT_BUILT, CREATED, HELP_INDEX, WITHOUT_ANIMATIONS, FULL};
BUILD_STATUS build_status() const { return build_status_; }
const std::vector<tportrait>& portraits() const { return portraits_; }
const config &get_cfg() const { return cfg_; }
@ -387,6 +396,7 @@ public:
const config::const_child_itors traits() const { return unit_cfg_->child_range("trait"); }
void set_config(config &cfg);
/// Finds a unit_type by its id() and makes sure it is built to the specified level.
const unit_type *find(const std::string &key, unit_type::BUILD_STATUS status = unit_type::FULL) const;
void check_types(const std::vector<std::string>& types) const;
const unit_race *find_race(const std::string &) const;
@ -408,7 +418,10 @@ private:
std::pair<unit_type_map::iterator, bool> insert(const std::pair<std::string, unit_type> &utype) { return types_.insert(utype); }
void clear();
unit_type& build_unit_type(const unit_type_map::iterator &ut, unit_type::BUILD_STATUS status) const;
/// Makes sure the provided unit_type is built to the specified level.
void build_unit_type(const unit_type & ut, unit_type::BUILD_STATUS status) const
{ ut.build(status, movement_types_, races_, unit_cfg_->child_range("trait")); }
void add_advancefrom(const config& unit_cfg) const;
void add_advancement(unit_type& to_unit) const;