/*
 * height_width.c
 *
 * Lib module to be inherited into humanoid npcs.
 * It allows width and height descriptions to be
 * set directly without having to juggle height
 * and width props.
 *
 * CAVEAT: You _must_ call set_height_desc() first, as the values generated
 *         by set_width_desc() are dependant on those values produced by
 *         set_height_desc(). You can call in either order, but calling
 *         set_height_desc() after may screw up the values generated by
 *         set_width_desc(). To be safe, use set_size_descs().
 */

#include <login.h>
#include <stdproperties.h>

/*
 * Function name: set_height_desc
 * Description  : Set the height property of the living based on the desired
 *                height description. If you also want to set he width, then
 *                it is important to first set the height before you set the
 *                width. Alternatively, use set_size_descs() to set both.
 *                To see all possible height descriptions, do:
 *                    exec return HEIGHTDESC;
 *
 * Arguments    : string str - the desired height description.
 * Returns      : int 1/0 - success/failure.
 */
public int
set_height_desc(string str)
{
    int attr;
    int index;
    int height;
    string race = query_race();

    if ((index = member_array(str, HEIGHTDESC)) < 0)
    {
        return 0;
    }

    if (member_array(race, RACES) < 0)
    {
        race = "human";
    }

    attr = RACEATTR[race][0];
    height = (attr * SPREAD_PROC[index]) / 100 - random(attr / 20);

    add_prop(CONT_I_HEIGHT, height);
    return 1;
}

/*
 * Function name: set_width_desc
 * Description  : Set the width property of the living based on the desired
 *                width description. If you also want to set the height, then
 *                it is important to first set the height before you set the
 *                width. Alternatively, use set_size_descs() to set both.
 *                To see all possible width descriptions, do:
 *                    exec return WIDTHDESC;
 *
 * Arguments    : string str - the desired width description.
 * Returns      : int 1/0 - success/failure.
 */
public int
set_width_desc(string str)
{
    int attr;
    int index;
    int width;
    string race = query_race();

    if ((index = member_array(str, WIDTHDESC)) < 0)
    {
        return 0;
    }

    if (member_array(race, RACES) < 0)
    {
        race = "human";
    }

    attr = RACEATTR[race][5];
    width = (attr * this_object()->query_prop(CONT_I_HEIGHT) *
        (index + 7) / 10);
    width -= random(width * attr / 8000);

    add_prop(CONT_I_WEIGHT, width);
    add_prop(CONT_I_VOLUME, width);
    return 1;
}

/*
 * Function name: set_size_descs
 * Description  : Call this function to set both the height and the width
 *                description. See set_height() and set_width() for details.
 * Arguments    : string height - the desired height description.
 *                string width  - the desired width description.
 * Returns      : int 1/0 - success/failure.
 */
public int
set_size_descs(string height, string width)
{
    if (!set_height_desc(height))
    {
        return 0;
    }

    if (!set_width_desc(width))
    {
        return 0;
    }

    return 1;
}

/*
 * Function name: set_random_width_desc
 * Description  : Call this function to set a random width description to
 *                the humanoid living.
 */
public int
set_random_width_desc()
{
    return set_width_desc(one_of_list(WIDTHDESC));
}

/*
 * Function name: set_random_height_desc
 * Description  : Call this function to set a random height description to
 *                the humanoid living.
 */
public int
set_random_height_desc()
{
    return set_height_desc(one_of_list(HEIGHTDESC));
}

/*
 * Function name: set_random_size_descs
 * Description  : Call this function to set a random width and random height
 *                description to the humanoid living.
 */
public int
set_random_size_descs()
{
    return set_size_descs(one_of_list(HEIGHTDESC), one_of_list(WIDTHDESC));
}
