Skip to content

Commit

Permalink
Add replacement method to requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingranade committed Jan 13, 2020
1 parent 161a9e0 commit 9ec036f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/requirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,42 @@ void requirement_data::blacklist_item( const std::string &id )
blacklisted |= apply_blacklist( components, id );
}

template <typename T>
static void apply_replacement( std::vector<std::vector<T>> &vec, const std::string &id,
const std::string &replacement )
{
// If the target and replacement are both present, remove the target.
// If only the target is present, replace it.
for( auto &opts : vec ) {
typename std::vector<T>::iterator target = opts.end();
typename std::vector<T>::iterator replacement_target = opts.end();
for( typename std::vector<T>::iterator iter = opts.begin(); iter != opts.end(); ++iter ) {
if( iter->type == id ) {
target = iter;
} else if( iter->type == replacement ) {
replacement_target = iter;
}
}
// No target to replace, do nothing.
if( target == opts.end() ) {
continue;
}
// Target but no replacement, replace.
if( replacement_target == opts.end() ) {
target->type = replacement;
continue;
}
// Both target and replacement, remove the target entry and leave the existing replacement.
opts.erase( target );
}
}

void requirement_data::replace_item( const itype_id &id, const itype_id &replacement )
{
apply_replacement( tools, id, replacement );
apply_replacement( components, id, replacement );
}

const requirement_data::alter_tool_comp_vector &requirement_data::get_tools() const
{
return tools;
Expand Down
5 changes: 4 additions & 1 deletion src/requirements.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ struct requirement_data {
return tools.empty() && components.empty() && qualities.empty();
}

/** check if removal of items via @ref blacklist_item left no alternatives in group */
bool is_blacklisted() const {
return blacklisted;
}
Expand Down Expand Up @@ -270,6 +269,10 @@ struct requirement_data {
* will be marked as @ref blacklisted
*/
void blacklist_item( const itype_id &id );
/**
* Replace tools or components of the given type.
*/
void replace_item( const itype_id &id, const itype_id &replacement );

const alter_tool_comp_vector &get_tools() const;
const alter_quali_req_vector &get_qualities() const;
Expand Down

0 comments on commit 9ec036f

Please sign in to comment.