forked from RoguelikeRestorationProject/urogue1.03
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathident.c
191 lines (162 loc) · 4.45 KB
/
ident.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
ident.c - This file contains routines to associate an identifier with an object.
UltraRogue
Copyright (C) 1984, 1985, 1986, 1987, 1990 Herb Chong
All rights reserved.
See the file LICENSE.TXT for full copyright and licensing information.
*/
/*
* ident
*
* This file contains routines to associate an identifier with an object. The
* identifiers are organized by type. Once an identifier is attached to an
* object, it remains with that object until the object is removed from the
* game. The identifiers are small integers, and they are assigned merely by
* counting objects of the same type. Allocation picks the next available
* integer.
*
* It is required that the linked list be sorted within types so that gaps can
* easily be detected.
*/
#include "rogue.h"
/*
* Index of 0 is invalid (unused state)
*/
char print_letters[] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
linked_list *ident_list = NULL; /* master list of all items */
/*
* get_ident()
*
* Gets the identifier for the given object. If an identifier exists, it is
* returned. If not, one is allocated and returned to the user. The
* identifier remains constant as long as the object is in the game.
*/
int
get_ident(struct object *obj_p)
{
int obj_type = obj_p->o_type;
linked_list *list_p; /* pointer into ident_list */
int new_id = 1; /* in case we have to allocate */
struct object *tmp_obj_p;
struct linked_list *new_place_p = NULL;
if (identifier(obj_p) != 0)
return (identifier(obj_p));
/*
* no identifier - must allocate one
*/
for (list_p = ident_list; list_p != NULL; list_p = next(list_p)) {
tmp_obj_p = OBJPTR(list_p);
if (tmp_obj_p->o_type == obj_type) {
if (identifier(tmp_obj_p) == new_id) {
/* if this id is taken, try next */
new_place_p = list_p;
new_id++;
}
}
}
/*
* If we get here, the object is not in the list, and we need to add
* it. The proper id is in new_id, and the place to put it is right
* after new_place_p.
*/
list_p = (struct linked_list *) new_alloc(sizeof(*list_p));
_attach_after(&ident_list, new_place_p, list_p);
identifier(obj_p) = new_id;
list_p->data.obj = obj_p;
return (new_id);
}
/*
* get_obj()
*
* get the object pointer, given the type and identifier.
*
* Returns NULL if no match was found
*/
object *
get_obj(int type, int ident)
{
linked_list *list_p;
object *obj_p;
for (list_p = ident_list; list_p != NULL; list_p = next(list_p)) {
obj_p = OBJPTR(list_p);
if (obj_p->o_type == type && identifier(obj_p) == ident)
return (obj_p);
}
return (NULL);
}
/*
* free_ident()
*
* Frees up an identifier by removing the list entry that contains that item. If
* the item isn't found, nothing is done.
*/
void
free_ident(struct object *obj_p)
{
linked_list *list_p, *nxt;
for (list_p = ident_list; list_p != NULL; list_p = nxt) {
nxt = next(list_p);
if (obj_p == OBJPTR(list_p)) {
_detach(&ident_list, list_p); /* unlink it from the
* list */
ur_free(list_p); /* release link structure */
}
}
}
/*
* unprint_id
*
* Converts a printable id from print_letters to the real thing by getting the
* index.
*/
int
unprint_id(char print_id)
{
char *id_p;
for (id_p = print_letters; *id_p; id_p++)
if (*id_p == print_id)
break;
return (id_p - print_letters);
}
/*
* max_print
*
* returns the size of the print list
*/
int
max_print()
{
return (sizeof(print_letters) - 2); /* 1 for blank and 1 for EOS
* string */
}
/*
* add_to_ident_list()
*
* Adds an object with an existing identifier to the ident list. Used when
* restoring saved games.
*/
void
add_to_ident_list(struct object *obj_p)
{
int obj_type = obj_p->o_type;
linked_list *list_p; /* pointer into ident_list */
int last_id = 0;
struct object *tmp_obj_p;
struct linked_list *new_place_p = NULL;
if (identifier(obj_p) == 0)
return; /* should never happen */
for (list_p = ident_list; list_p != NULL; list_p = next(list_p)) {
tmp_obj_p = OBJPTR(list_p);
if (tmp_obj_p->o_type == obj_type) {
if (identifier(tmp_obj_p) > identifier(obj_p))
break;
else if (identifier(tmp_obj_p) >= last_id) {
new_place_p = list_p;
last_id = identifier(tmp_obj_p);
}
}
}
list_p = (struct linked_list *) new_alloc(sizeof(*list_p));
_attach_after(&ident_list, new_place_p, list_p);
list_p->data.obj = obj_p;
}