Changeset 854

Show
Ignore:
Timestamp:
04/28/08 22:40:18 (5 months ago)
Author:
takkaria
Message:

Add some extra item lookup functions so that eventually textual tvals and svals can be specified in the edit files.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/obj-util.c

    r828 r854  
    19411941 
    19421942/* 
    1943  * Find the index of the object_kind with the given tval and sval 
    1944  */ 
    1945 s16b lookup_kind(int tval, int sval) 
    1946 { 
    1947         int k; 
    1948  
    1949         /* Look for it */ 
    1950         for (k = 1; k < z_info->k_max; k++) 
    1951         { 
    1952                 object_kind *k_ptr = &k_info[k]; 
    1953  
    1954                 /* Found a match */ 
    1955                 if ((k_ptr->tval == tval) && (k_ptr->sval == sval)) return (k); 
    1956         } 
    1957  
    1958         /* Oops */ 
    1959         msg_format("No object (%d,%d)", tval, sval); 
    1960  
    1961         /* Oops */ 
    1962         return (0); 
    1963 } 
    1964  
    1965  
    1966 /** 
    1967  * Find the tval and sval of object kind `k_idx`, and return via the pointers 
    1968  * `tval` and `sval`. 
    1969  */ 
    1970 bool lookup_reverse(s16b k_idx, int *tval, int *sval) 
    1971 { 
    1972         object_kind *k_ptr; 
    1973  
    1974         /* Validate k_idx */ 
    1975         if ((k_idx < 1) || (k_idx > z_info->k_max)) 
    1976                 return FALSE; 
    1977  
    1978         /* Get pointer */ 
    1979         k_ptr = &k_info[k_idx]; 
    1980         *tval = k_ptr->tval; 
    1981         *sval = k_ptr->sval; 
    1982  
    1983         /* Done */ 
    1984         return TRUE; 
    1985 } 
    1986  
    1987  
    1988 /* 
    19891943 * Wipe an object clean. 
    19901944 */ 
     
    34043358        return i; 
    34053359} 
     3360 
     3361/*** Object kind lookup functions ***/ 
     3362 
     3363/** 
     3364 * Return the k_idx of the object kind with the given `tval` and `sval`, or 0. 
     3365 */ 
     3366int lookup_kind(int tval, int sval) 
     3367{ 
     3368        int k; 
     3369 
     3370        /* Look for it */ 
     3371        for (k = 1; k < z_info->k_max; k++) 
     3372        { 
     3373                object_kind *k_ptr = &k_info[k]; 
     3374 
     3375                /* Found a match */ 
     3376                if ((k_ptr->tval == tval) && (k_ptr->sval == sval)) return (k); 
     3377        } 
     3378 
     3379        /* Failure */ 
     3380        msg_format("No object (%s,%d)", tval_find_name(tval), tval, sval); 
     3381        return 0; 
     3382} 
     3383 
     3384 
     3385/** 
     3386 * Find the tval and sval of object kind `k_idx`, and return via the pointers 
     3387 * `tval` and `sval`. 
     3388 */ 
     3389bool lookup_reverse(s16b k_idx, int *tval, int *sval) 
     3390{ 
     3391        object_kind *k_ptr; 
     3392 
     3393        /* Validate k_idx */ 
     3394        if ((k_idx < 1) || (k_idx > z_info->k_max)) 
     3395                return FALSE; 
     3396 
     3397        /* Get pointer */ 
     3398        k_ptr = &k_info[k_idx]; 
     3399        *tval = k_ptr->tval; 
     3400        *sval = k_ptr->sval; 
     3401 
     3402        /* Done */ 
     3403        return TRUE; 
     3404} 
     3405 
     3406 
     3407/*** Textual<->numeric conversion ***/ 
     3408 
     3409/** 
     3410 * List of { tval, name } pairs. 
     3411 */ 
     3412static const grouper tval_names[] = 
     3413{ 
     3414        { TV_SKELETON,    "skeleton" }, 
     3415        { TV_BOTTLE,      "bottle" }, 
     3416        { TV_JUNK,        "junk" }, 
     3417        { TV_SPIKE,       "spike" }, 
     3418        { TV_CHEST,       "chest" }, 
     3419        { TV_SHOT,        "shot" }, 
     3420        { TV_ARROW,       "arrow" }, 
     3421        { TV_BOLT,        "bolt" }, 
     3422        { TV_BOW,         "bow" }, 
     3423        { TV_DIGGING,     "digger" }, 
     3424        { TV_HAFTED,      "hafted" }, 
     3425        { TV_POLEARM,     "polearm" }, 
     3426        { TV_SWORD,       "sword" }, 
     3427        { TV_BOOTS,       "boots" }, 
     3428        { TV_GLOVES,      "gloves" }, 
     3429        { TV_HELM,        "helm" }, 
     3430        { TV_CROWN,       "crown" }, 
     3431        { TV_SHIELD,      "shield" }, 
     3432        { TV_CLOAK,       "cloak" }, 
     3433        { TV_SOFT_ARMOR,  "soft armor" }, 
     3434        { TV_SOFT_ARMOR,  "soft armour" }, 
     3435        { TV_HARD_ARMOR,  "hard armor" }, 
     3436        { TV_HARD_ARMOR,  "hard armour" }, 
     3437        { TV_DRAG_ARMOR,  "dragon armor" }, 
     3438        { TV_DRAG_ARMOR,  "dragon armour" }, 
     3439        { TV_LITE,        "light" }, 
     3440        { TV_AMULET,      "amulet" }, 
     3441        { TV_RING,        "ring" }, 
     3442        { TV_STAFF,       "staff" }, 
     3443        { TV_WAND,        "wand" }, 
     3444        { TV_ROD,         "rod" }, 
     3445        { TV_SCROLL,      "scroll" }, 
     3446        { TV_POTION,      "potion" }, 
     3447        { TV_FLASK,       "flask" }, 
     3448        { TV_FOOD,        "food" }, 
     3449        { TV_MAGIC_BOOK,  "magic book" }, 
     3450        { TV_PRAYER_BOOK, "prayer book" }, 
     3451        { TV_GOLD,        "gold" }, 
     3452}; 
     3453 
     3454/** 
     3455 * Return the k_idx of the object kind with the given `tval` and name `name`. 
     3456 */ 
     3457int lookup_name(int tval, const char *name) 
     3458{ 
     3459        int k; 
     3460 
     3461        /* Look for it */ 
     3462        for (k = 1; k < z_info->k_max; k++) 
     3463        { 
     3464                object_kind *k_ptr = &k_info[k]; 
     3465                const char *nm = k_name + k_ptr->name; 
     3466 
     3467                if (*nm == '&' && *(nm+1)) 
     3468                        nm += 2; 
     3469 
     3470                /* Found a match */ 
     3471                if (k_ptr->tval == tval && !strcmp(name, nm)) 
     3472                        return k; 
     3473        } 
     3474 
     3475        msg_format("No object (\"%s\",\"%s\")", tval_find_name(tval), name); 
     3476        return 0; 
     3477} 
     3478 
     3479/** 
     3480 * Returns the numeric equivalent tval of the textual tval `name`. 
     3481 */ 
     3482int tval_find_idx(const char *name) 
     3483{ 
     3484        size_t i = 0; 
     3485 
     3486        for (i = 0; i < N_ELEMENTS(tval_names); i++) 
     3487        { 
     3488                if (!my_stricmp(name, tval_names[i].name)) 
     3489                        return tval_names[i].tval; 
     3490        } 
     3491 
     3492        return -1; 
     3493} 
     3494 
     3495/** 
     3496 * Returns the textual equivalent tval of the numeric tval `name`. 
     3497 */ 
     3498const char *tval_find_name(int tval) 
     3499{ 
     3500        size_t i = 0; 
     3501 
     3502        for (i = 0; i < N_ELEMENTS(tval_names); i++) 
     3503        { 
     3504                if (tval == tval_names[i].tval) 
     3505                        return tval_names[i].name; 
     3506        } 
     3507 
     3508        return "unknown"; 
     3509} 
     3510 
  • trunk/src/object.h

    r828 r854  
    107107bool object_similar(const object_type *o_ptr, const object_type *j_ptr); 
    108108void object_absorb(object_type *o_ptr, const object_type *j_ptr); 
    109 s16b lookup_kind(int tval, int sval); 
    110 bool lookup_reverse(s16b k_idx, int *tval, int *sval); 
    111109void object_wipe(object_type *o_ptr); 
    112110void object_copy(object_type *o_ptr, const object_type *j_ptr); 
     
    138136void reduce_charges(object_type *o_ptr, int amt); 
    139137unsigned check_for_inscrip(const object_type *o_ptr, const char *inscrip); 
     138int lookup_kind(int tval, int sval); 
     139bool lookup_reverse(s16b k_idx, int *tval, int *sval); 
     140int lookup_name(int tval, const char *name); 
     141int tval_find_idx(const char *name); 
     142const char *tval_find_name(int tval); 
    140143 
    141144#endif /* !INCLUDED_OBJECT_H */