/**
 * Given a vpart id, gives the choice of inventory and nearby items to consume
 * for install/repair/etc. Doesn't use consume_items in crafting.cpp, as it got
 * into weird cases and doesn't consider properties like HP and bigness. The
 * item will be removed by this function.
 * @param vpid The id of the vpart type to look for.
 * @return The item that was consumed.
 */
item consume_vpart_item (game *g, std::string vpid)
{
    std::vector<candidate_vpart> candidates;
    const itype_id itid = vehicle_part_types[vpid].item;
    for (int x = g->u.posx - PICKUP_RANGE; x <= g->u.posx + PICKUP_RANGE; x++)
    {
        for (int y = g->u.posy - PICKUP_RANGE; y <= g->u.posy + PICKUP_RANGE; y++)
        {
            for(int i=0; i < g->m.i_at(x,y).size(); i++)
            {
                item* ith_item = &(g->m.i_at(x,y)[i]);
                if (ith_item->type->id == itid)
                {
                    candidates.push_back (candidate_vpart(x,y,i,*ith_item));
                }
            }
        }
    }

    std::vector<item*> cand_from_inv = g->u.inv.all_items_by_type(itid);
    for (int i=0; i < cand_from_inv.size(); i++)
    {
        item* ith_item = cand_from_inv[i];
        if (ith_item->type->id  == itid)
        {
            candidates.push_back (candidate_vpart(ith_item->invlet,*ith_item));
        }
    }
    if (g->u.weapon.type->id == itid) {
        candidates.push_back (candidate_vpart(-1,g->u.weapon));
    }

    // bug?
    if(candidates.size() == 0)
    {
        debugmsg("part not found");
        return item();
    }

    int selection;
    // no choice?
    if(candidates.size() == 1) {
        selection = 0;
    } else {
        // popup menu!?
        std::vector<std::string> options;
        for(int i=0;i<candidates.size(); i++)
        {
            if(candidates[i].in_inventory)
            {
                if (candidates[i].invlet == -1)
                {
                    options.push_back(candidates[i].vpart_item.tname() + _(" (wielded)"));
                }
                else
                {
                    options.push_back(candidates[i].vpart_item.tname());
                }
            }
            else
            { //nearby.
                options.push_back(candidates[i].vpart_item.tname() + _(" (nearby)"));
            }
        }
        selection = menu_vec(false, _("Use which gizmo?"), options);
        selection -= 1;
    }
    //remove item from inventory. or map.
    if(candidates[selection].in_inventory)
    {
        if(candidates[selection].invlet == -1) //weapon
        {
            g->u.remove_weapon();
        }
        else //non-weapon inventory
        {
            g->u.inv.remove_item_by_letter(candidates[selection].invlet);
        }
    } 
    else
    { //map.
        int x = candidates[selection].mapx;
        int y = candidates[selection].mapy;
        int i = candidates[selection].index;
        g->m.i_rem(x,y,i);
    }
    return candidates[selection].vpart_item;
}
// given vpart type, give a choice from inventory items & nearby items.
// not using consume_items in crafting.cpp
// because it got into weird cases, & it doesn't consider
// characteristics like item hp & bigness.
item consume_vpart_item (game *g, vpart_id vpid){
    std::vector<candidate_vpart> candidates;
    const itype_id itid = vpart_list[vpid].item;
    for (int x = g->u.posx - PICKUP_RANGE; x <= g->u.posx + PICKUP_RANGE; x++)
       for (int y = g->u.posy - PICKUP_RANGE; y <= g->u.posy + PICKUP_RANGE; y++)
          for(int i=0; i < g->m.i_at(x,y).size(); i++){
             item* ith_item = &(g->m.i_at(x,y)[i]);
             if (ith_item->type->id == itid)
                candidates.push_back (candidate_vpart(x,y,i,*ith_item));
          }

    for (int i=0; i<g->u.inv.size(); i++){
       item* ith_item = &(g->u.inv[i]);
       if (ith_item->type->id  == itid)
          candidates.push_back (candidate_vpart(i,*ith_item));
    }
    if (g->u.weapon.type->id == itid) {
       candidates.push_back (candidate_vpart(-1,g->u.weapon));
    }

    // bug?
    if(candidates.size() == 0){
       debugmsg("part not found");
       return item();
    }

    int selection;
    // no choice?
    if(candidates.size() == 1) {
       selection = 0;
    } else {
       // popup menu!?
       std::vector<std::string> options;
       for(int i=0;i<candidates.size(); i++){
          if(candidates[i].in_inventory){
             if (candidates[i].index == -1)
                options.push_back(candidates[i].vpart_item.tname() + " (wielded)");
             else
                options.push_back(candidates[i].vpart_item.tname());
          }
          else { //nearby.
             options.push_back(candidates[i].vpart_item.tname() + " (nearby)");
          }
       }
       selection = menu_vec("Use which gizmo?", options);
       selection -= 1;
    }
    //remove item from inventory. or map.
    if(candidates[selection].in_inventory){
       if(candidates[selection].index == -1) //weapon
          g->u.remove_weapon();
       else //non-weapon inventory
          g->u.inv.remove_item (candidates[selection].index);
    } else { //map.
       int x = candidates[selection].mapx;
       int y = candidates[selection].mapy;
       int i = candidates[selection].index;
       g->m.i_rem(x,y,i);
    }
    return candidates[selection].vpart_item;
    //item ret = candidates[selection].vpart_item;
    //return ret;
}