Skip to content

Commit

Permalink
Revert change to requirements.h/.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Coolthulhu committed Jun 30, 2016
1 parent e693e87 commit 81f0b9a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 84 deletions.
67 changes: 3 additions & 64 deletions src/requirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,9 @@ void item_comp::load( JsonArray &ja )
JsonArray comp = ja.next_array();
type = comp.get_string( 0 );
count = comp.get_int( 1 );
if( comp.size() == 3 ) {
const std::string param_string = comp.get_string( 2 );
if( param_string == "NO_RECOVER" ) {
// Recoverable is true by default.
recoverable = false;
} else if( param_string == "LIST" ) {
is_nested_requirement = true;
}
// Recoverable is true by default.
if( comp.size() > 2 ) {
recoverable = comp.get_string( 2 ) == "NO_RECOVER" ? false : true;
}
if( count <= 0 ) {
ja.throw_error( "item count must be a positive number" );
Expand Down Expand Up @@ -763,59 +758,3 @@ const requirement_data requirement_data::disassembly_requirements() const

return ret;
}

template <typename T>
void requirement_data::inline_nested( std::vector< std::vector<T> > &vec )
{
// @todo Inlining whole alternatives
const auto find_nested = []( const std::vector<T> &v ) {
return std::find_if( v.begin(), v.end(), []( const T &comp ) {
return comp.is_nested_requirement;
} );
};
for( auto &alt : vec ) {
auto iter = find_nested( alt );

while( iter != alt.end() ) {
// This is an ugly id conversion thing
// It should be done better, if it is possible
requirement_id inlined_id( iter->type );
if( !inlined_id.is_valid() ) {
debugmsg( "Tried to inline invalid requirement %s", inlined_id.c_str() );
continue;
}

const requirement_data inlined = (*inlined_id) * iter->count;
inlined.inline_into( alt );

iter = find_nested( alt );
}
}
}

template<>
void requirement_data::inline_into( std::vector<tool_comp> &vec ) const
{
const auto &tool = tools.front();
vec.insert( vec.end(), tool.begin(), tool.end() );
}

template<>
void requirement_data::inline_into( std::vector<item_comp> &vec ) const
{
const auto &comp = components.front();
vec.insert( vec.end(), comp.begin(), comp.end() );
}

void requirement_data::finalize_this()
{
inline_nested( components );
inline_nested( tools );
}

void requirement_data::finalize()
{
for( auto &pr : requirements_all ) {
pr.second.finalize_this();
}
}
33 changes: 13 additions & 20 deletions src/requirements.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ struct quality {
};

struct component {
itype_id type = "null";
int count = 0;
itype_id type;
int count;
// -1 means the player doesn't have the item, 1 means they do,
// 0 means they have item but not enough for both tool and component
mutable available_status available = a_false;
bool recoverable = true;
// Not a real component, but a fake one used for nesting
bool is_nested_requirement = false;

component() { }
component( const itype_id &TYPE, int COUNT ) : type( TYPE ), count( COUNT ) { }
component( const itype_id &TYPE, int COUNT, bool RECOVERABLE ) :
type( TYPE ), count( COUNT ), recoverable( RECOVERABLE ) { }
mutable available_status available;
bool recoverable;

component() : type( "null" ) , count( 0 ) , available( a_false ), recoverable( true ) {
}
component( const itype_id &TYPE, int COUNT ) : type( TYPE ), count( COUNT ), available( a_false ),
recoverable( true ) {
}
component( const itype_id &TYPE, int COUNT, bool RECOVERABLE ) : type( TYPE ), count( COUNT ),
available( a_false ), recoverable( RECOVERABLE ) {
}
void check_consistency( const std::string &display_name ) const;
};

Expand Down Expand Up @@ -179,9 +181,6 @@ struct requirement_data {
/** Get all currently loaded requirements */
static const std::map<requirement_id, requirement_data> &all();

/** Finalize all loaded requirements */
static void finalize();

/** Check consistency of all loaded requirements */
static void check_consistency();

Expand Down Expand Up @@ -222,8 +221,6 @@ struct requirement_data {
private:
requirement_id id_ = requirement_id( "null" );

void finalize_this();

bool check_enough_materials( const inventory &crafting_inv, int batch = 1 ) const;
bool check_enough_materials( const item_comp &comp, const inventory &crafting_inv,
int batch = 1 ) const;
Expand Down Expand Up @@ -251,10 +248,6 @@ struct requirement_data {
static void load_obj_list( JsonArray &jsarr, std::vector< std::vector<T> > &objs );
template<typename T, typename ID>
static const T *find_by_type( const std::vector< std::vector<T> > &vec, const ID &type );
template<typename T>
void inline_nested( std::vector< std::vector<T> > &vec );
template<typename T>
void inline_into( std::vector<T> &vec ) const;
};

#endif

0 comments on commit 81f0b9a

Please sign in to comment.