/* This is the bulk of a command to show you what equipment is in which take slots, for what levels. Useful for
working out what exists, and therefore what objects are needed. Remember to add it to the command
list and interpreter. This is the section for act_wizard.c 

Syntax: Wearlist <wearslot> <lowlevel> <highlevel> 

Standard Disclaimer: Athravan used to be creator, owner and implementor of Visions of Eternity. This was a MUD
which had some 10 coders working on it over it's 3 year lifespan. Although she was main coder and implementor,
I give credit to anyone else who may have worked on VoE, primarily Sonyc. There may be some bugs/errors - this 
code hasn't been on an active mud since 2003.

Please contact me in the event of any bugs on athravan@ntlworld.com or at Dragon Swords MUD (www.dragonswords.net)*/

ACMD(do_wearlist)
{
    const char *overflow = "\r\n**OVERFLOW**\r\n";
    char arg_wear[MAX_INPUT_LENGTH], arg_lvl1[MAX_INPUT_LENGTH], arg_lvl2[MAX_INPUT_LENGTH];
    int wear_location, level_low, level_high, level, next_level, i;
    char result[MAX_PAGE_STRING_LENGTH];
    size_t len = 0, nlen;


    // First argument should be the wear location
    argument = one_argument(argument, arg_wear);
    // Then the low and high level
    argument = two_arguments(argument, arg_lvl1, arg_lvl2);

    // Verify presence of all arguments
    if(!*arg_wear || (*arg_lvl1 && !*arg_lvl2))
    {
        send_to_char(ch, "Usage: wearlist <wear location> [<lowest level> "
            "<highest level>]\r\n");
        return;
    }
    
    // Lookup which wear location is in
    for(wear_location = 0; wear_location < wear_bits_count; ++wear_location)
    {
        if(!str_cmp(arg_wear, wear_bits[wear_location]))
            break;
    }

    // Check if we got a valid wear location
    if(wear_location == wear_bits_count)
    {
        send_to_char(ch, "Possible wear locations are:\r\n");
        for(wear_location = 0; wear_location < wear_bits_count; ++wear_location)
        {
            send_to_char(ch, wear_bits[wear_location]);
            send_to_char(ch, " ");
        }
        send_to_char(ch, "\r\n");
        return;
    }

    level_low = atoi(arg_lvl1);
    if(level_low == 0)
        level_low = 1;

    level_high = atoi(arg_lvl2);
    if(level_high == 0)
        level_high = LVL_IMPL;

    // Check that levels are valid
    if( level_low < 0 || level_low > LVL_IMPL || 
        level_high < 0 || level_high > LVL_IMPL ||
        level_high < level_low)
    {
        send_to_char(ch, "Invalid levels.\r\n");
        return;
    }

    for(level = level_low; level <= level_high; level = next_level)
    {
        next_level = level_high;

        for(i = 0; i < top_of_objt; ++i)
        {
            if( GET_OBJ_LEVEL(obj_proto + i) > level &&
                GET_OBJ_LEVEL(obj_proto + i) < next_level)
            {
                next_level = GET_OBJ_LEVEL(obj_proto + i);
            }

            if( CAN_WEAR(obj_proto + i, wear_location) && 
                GET_OBJ_LEVEL(obj_proto + i) == level)
            {
                nlen = snprintf(result + len, sizeof(result) - len, "Level %-5d: [%5d] %s\r\n", 
                    GET_OBJ_LEVEL(obj_proto + i), GET_OBJ_VNUM(obj_proto + i), 
                    obj_proto[i].short_description);

                len += nlen;
                if(len >= sizeof(result))
                    break;
            }
        }
        
        if(level == next_level)
            break;
    }

    // Report overflows
    if (len >= sizeof(result))
        strcpy(result + sizeof(result) - strlen(overflow) - 1, overflow); /* strcpy: OK */

    if(len == 0)
        send_to_char(ch, "No objects found.");
    else
        page_string(ch->desc, result, TRUE);  
}