bool requirements::check_enough_materials( const item_comp& comp, const inventory& crafting_inv ) const { if( comp.available != a_true ) { return false; } const itype *it = item_controller->find_template( comp.type ); const tool_comp *tq = find_by_type( tools, comp.type ); if( tq != nullptr ) { // The very same item type is also needed as tool! // Use charges of it, or use it by count? const int tc = tq->count < 0 ? std::abs( tq->count ) : 1; // Check for components + tool count. Check item amount (excludes // pseudo items) and tool amount (includes pseudo items) // Imagine: required = 1 welder (component) + 1 welder (tool), // available = 1 welder (real item), 1 welding rig (creates // a pseudo welder item). has_components(welder,2) returns false // as there is only one real welder available, but has_tools(welder,2) // returns true. // Keep in mind that both requirements (tool+component) are checked // before this. That assures that one real item is actually available, // two welding rigs (and no real welder) would make this component // non-available even before this function is called. // Only ammo and (some) food is counted by charges, both are unlikely // to appear as tool, but it's possible /-: bool has_comps; if( it->count_by_charges() && comp.count > 0 ) { has_comps = crafting_inv.has_charges( comp.type, comp.count + tc ); } else { has_comps = crafting_inv.has_components( comp.type, abs( comp.count ) + tc ); } if( !has_comps && !crafting_inv.has_tools( comp.type, comp.count + tc ) ) { comp.available = a_insufficent; } } for( const auto &ql : it->qualities ) { const quality_requirement *qr = find_by_type( qualities, ql.first ); if( qr == nullptr || qr->level > ql.second ) { continue; } // This item can be used for the quality requirement, same as above for specific // tools applies. if( !crafting_inv.has_items_with_quality( qr->type, qr->level, qr->count + abs(comp.count) ) ) { comp.available = a_insufficent; } } return comp.available == a_true; }
bool quality_requirement::has( const inventory &crafting_inv, int ) const { return crafting_inv.has_items_with_quality( type, level, count ); }