Annotation of early-roguelike/urogue/ident.c, Revision 1.1
1.1 ! rubenllo 1: /*
! 2: ident.c - routines to associate an identifier with an object
! 3:
! 4: UltraRogue: The Ultimate Adventure in the Dungeons of Doom
! 5: Copyright (C) 1986, 1991, 1993, 1995 Herb Chong
! 6: All rights reserved.
! 7:
! 8: See the file LICENSE.TXT for full copyright and licensing information.
! 9: */
! 10:
! 11: /*
! 12: * ident
! 13: *
! 14: * This file contains routines to associate an identifier with an object. The
! 15: * identifiers are organized by type. Once an identifier is attached to an
! 16: * object, it remains with that object until the object is removed from the
! 17: * game. The identifiers are small integers, and they are assigned merely by
! 18: * counting objects of the same type. Allocation picks the next available
! 19: * integer.
! 20: *
! 21: * It is required that the linked list be sorted within types so that gaps can
! 22: * easily be detected.
! 23: */
! 24:
! 25: #include "rogue.h"
! 26:
! 27: /*
! 28: * Index of 0 is invalid (unused state)
! 29: */
! 30:
! 31: char print_letters[] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
! 32: linked_list *ident_list = NULL; /* master list of all items */
! 33:
! 34: /*
! 35: get_ident()
! 36:
! 37: Gets the identifier for the given object. If an identifier exists, it is
! 38: returned. If not, one is allocated and returned to the user. The
! 39: identifier remains constant as long as the object is in the game.
! 40: */
! 41:
! 42: int
! 43: get_ident(struct object *obj_p)
! 44: {
! 45: int obj_type = obj_p->o_type;
! 46: linked_list *list_p; /* pointer into ident_list */
! 47: int new_id = 1; /* in case we have to allocate */
! 48: struct object *tmp_obj_p;
! 49: struct linked_list *new_place_p = NULL;
! 50:
! 51: if (identifier(obj_p) != 0)
! 52: return (identifier(obj_p));
! 53:
! 54: /* no identifier - must allocate one */
! 55:
! 56: for (list_p = ident_list; list_p != NULL; list_p = next(list_p))
! 57: {
! 58: tmp_obj_p = OBJPTR(list_p);
! 59:
! 60: if (tmp_obj_p->o_type == obj_type)
! 61: {
! 62: if (identifier(tmp_obj_p) == new_id)
! 63: {
! 64: /* if this id is taken, try next */
! 65: new_place_p = list_p;
! 66: new_id++;
! 67: }
! 68: }
! 69: }
! 70:
! 71: /*
! 72: * If we get here, the object is not in the list, and we need to add
! 73: * it. The proper id is in new_id, and the place to put it is right
! 74: * after new_place_p.
! 75: */
! 76:
! 77: list_p = new_list();
! 78: _attach_after(&ident_list, new_place_p, list_p);
! 79: identifier(obj_p) = new_id;
! 80: list_p->data.obj = obj_p;
! 81: return(new_id);
! 82: }
! 83:
! 84: /*
! 85: free_ident()
! 86:
! 87: Frees up an identifier by removing the list entry that contains that item.
! 88: If the item isn't found, nothing is done.
! 89: */
! 90:
! 91: void
! 92: free_ident(struct object *obj_p)
! 93: {
! 94: linked_list *list_p;
! 95:
! 96: for (list_p = ident_list; list_p != NULL; list_p = next(list_p))
! 97: {
! 98: if (obj_p == OBJPTR(list_p))
! 99: {
! 100: _detach(&ident_list, list_p); /* unlink it from the list */
! 101: ur_free(list_p); /* release link structure */
! 102: break;
! 103: }
! 104: }
! 105: }
! 106:
! 107: /*
! 108: unprint_id()
! 109:
! 110: Converts a printable id from print_letters to the real thing by getting the
! 111: index.
! 112: */
! 113:
! 114: int
! 115: unprint_id(char *print_id)
! 116: {
! 117: char *id_p;
! 118:
! 119: for (id_p = print_letters; id_p != NULL; id_p++)
! 120: if (*id_p == *print_id)
! 121: break;
! 122:
! 123: return( (int) (id_p - print_letters) );
! 124: }
! 125:
! 126: /*
! 127: max_print()
! 128:
! 129: returns the size of the print list
! 130: */
! 131:
! 132: int
! 133: max_print(void)
! 134: {
! 135: return(sizeof(print_letters) - 2); /* 1 for blank and 1 for EOS string */
! 136: }
CVSweb