Example #1
0
std::list<item> craft_command::consume_components()
{
    std::list<item> used;
    if( crafter->has_trait( trait_id( "DEBUG_HS" ) ) ) {
        return used;
    }

    if( empty() ) {
        debugmsg( "Warning: attempted to consume items from an empty craft_command" );
        return used;
    }

    inventory map_inv;
    map_inv.form_from_map( crafter->pos(), PICKUP_RANGE );

    if( !check_item_components_missing( map_inv ).empty() ) {
        debugmsg( "Aborting crafting: couldn't find cached components" );
        return used;
    }

    for( const auto &it : item_selections ) {
        std::list<item> tmp = crafter->consume_items( it, batch_size );
        used.splice( used.end(), tmp );
    }

    for( const auto &it : tool_selections ) {
        crafter->consume_tools( it, batch_size );
    }

    return used;
}
void craft_command::execute()
{
    if( empty() ) {
        return;
    }

    bool need_selections = true;
    inventory map_inv;
    map_inv.form_from_map( crafter->pos(), PICKUP_RANGE );

    if( has_cached_selections() ) {
        std::vector<comp_selection<item_comp>> missing_items = check_item_components_missing( map_inv );
        std::vector<comp_selection<tool_comp>> missing_tools = check_tool_components_missing( map_inv );

        if( missing_items.empty() && missing_tools.empty() ) {
            // All items we used previously are still there, so we don't need to do selection.
            need_selections = false;
        } else if( !query_continue( missing_items, missing_tools ) ) {
            return;
        }
    }

    const auto needs = rec->requirements();

    if( need_selections ) {
        item_selections.clear();
        for( const auto &it : needs.get_components() ) {
            comp_selection<item_comp> is = crafter->select_item_component( it, batch_size, map_inv, true );
            if( is.use_from == cancel ) {
                return;
            }
            item_selections.push_back( is );
        }

        tool_selections.clear();
        for( const auto &it : needs.get_tools() ) {
            comp_selection<tool_comp> ts = crafter->select_tool_component(
                                               it, batch_size, map_inv, DEFAULT_HOTKEYS, true );
            if( ts.use_from == cancel ) {
                return;
            }
            tool_selections.push_back( ts );
        }
    }

    auto activity = player_activity( is_long ? ACT_LONGCRAFT : ACT_CRAFT,
                                     rec->batch_time( batch_size ),
                                     -1, INT_MIN, rec->ident() );
    activity.values.push_back( batch_size );

    crafter->assign_activity( activity );

    /* legacy support for lua bindings to last_batch and lastrecipe */
    crafter->last_batch = batch_size;
    crafter->lastrecipe = rec->ident();
}