comparison xrogue/list.c @ 133:e6179860cb76

Import XRogue 8.0 from the Roguelike Restoration Project (r1490)
author John "Elwin" Edwards
date Tue, 21 Apr 2015 08:55:20 -0400
parents
children ce0cf824c192
comparison
equal deleted inserted replaced
124:d10fc4a065ac 133:e6179860cb76
1 /*
2 list.c - Functions for dealing with linked lists of goodies
3
4 XRogue: Expeditions into the Dungeons of Doom
5 Copyright (C) 1991 Robert Pietkivitch
6 All rights reserved.
7
8 Based on "Advanced Rogue"
9 Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka and AT&T
10 All rights reserved.
11
12 Based on "Rogue: Exploring the Dungeons of Doom"
13 Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
14 All rights reserved.
15
16 See the file LICENSE.TXT for full copyright and licensing information.
17 */
18
19 #include <stdlib.h>
20 #include <curses.h>
21 #include "rogue.h"
22
23 /*
24 * detach:
25 * Takes an item out of whatever linked list it might be in
26 */
27
28 _detach(list, item)
29 register struct linked_list **list, *item;
30 {
31 if (*list == item)
32 *list = next(item);
33 if (prev(item) != NULL) item->l_prev->l_next = next(item);
34 if (next(item) != NULL) item->l_next->l_prev = prev(item);
35 item->l_next = NULL;
36 item->l_prev = NULL;
37 }
38
39 /*
40 * _attach:
41 * add an item to the head of a list
42 */
43
44 _attach(list, item)
45 register struct linked_list **list, *item;
46 {
47 if (*list != NULL)
48 {
49 item->l_next = *list;
50 (*list)->l_prev = item;
51 item->l_prev = NULL;
52 }
53 else
54 {
55 item->l_next = NULL;
56 item->l_prev = NULL;
57 }
58
59 *list = item;
60 }
61
62 /*
63 * o_free_list:
64 * Throw the whole object list away
65 */
66
67 _o_free_list(ptr)
68 register struct linked_list **ptr;
69 {
70 register struct linked_list *item;
71
72 while (*ptr != NULL)
73 {
74 item = *ptr;
75 *ptr = next(item);
76 o_discard(item);
77 }
78 }
79
80 /*
81 * o_discard:
82 * free up an item and its object(and maybe contents)
83 */
84
85 o_discard(item)
86 register struct linked_list *item;
87 {
88 register struct object *obj;
89
90 obj = OBJPTR(item);
91 if (obj->contents != NULL)
92 o_free_list(obj->contents);
93 total -= 2;
94 FREE(obj);
95 FREE(item);
96 }
97
98 /*
99 r_free_fire_list
100 Throw the whole list of fire monsters away. But don't
101 discard the item (monster) itself as that belong to mlist.
102 */
103
104 _r_free_fire_list(ptr)
105 register struct linked_list **ptr;
106 {
107 register struct linked_list *item;
108
109 while (*ptr != NULL)
110 {
111 item = *ptr;
112 *ptr = next(item);
113 free(item);
114 }
115 }
116 /*
117 * r_free_list:
118 * Throw the whole list of room exits away
119 */
120
121 _r_free_list(ptr)
122 register struct linked_list **ptr;
123 {
124 register struct linked_list *item;
125
126 while (*ptr != NULL)
127 {
128 item = *ptr;
129 *ptr = next(item);
130 r_discard(item);
131 }
132 }
133
134 /*
135 * r_discard:
136 * free up an item and its room
137 */
138
139 r_discard(item)
140 register struct linked_list *item;
141 {
142 total -= 2;
143 FREE(DOORPTR(item));
144 FREE(item);
145 }
146
147 /*
148 * t_free_list:
149 * Throw the whole thing list away
150 */
151
152 _t_free_list(ptr)
153 register struct linked_list **ptr;
154 {
155 register struct linked_list *item;
156
157 while (*ptr != NULL)
158 {
159 item = *ptr;
160 *ptr = next(item);
161 t_discard(item);
162 }
163 }
164
165 /*
166 * t_discard:
167 * free up an item and its thing
168 */
169
170 t_discard(item)
171 register struct linked_list *item;
172 {
173 register struct thing *tp;
174
175 total -= 2;
176 tp = THINGPTR(item);
177 if (tp->t_name != NULL) FREE(tp->t_name);
178 if (tp->t_pack != NULL)
179 o_free_list(tp->t_pack);
180 FREE(tp);
181 FREE(item);
182 }
183
184 /*
185 * destroy_item:
186 * get rid of an item structure -- don't worry about contents
187 */
188
189 destroy_item(item)
190 register struct linked_list *item;
191 {
192 total--;
193 FREE(item);
194 }
195
196 /*
197 * new_item
198 * get a new item with a specified size
199 */
200
201 struct linked_list *
202 new_item(size)
203 int size;
204 {
205 register struct linked_list *item;
206
207 if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
208 msg("Ran out of memory for header after %d items", total);
209 if ((item->l_data = new(size)) == NULL)
210 msg("Ran out of memory for data after %d items", total);
211 item->l_next = item->l_prev = NULL;
212 memset(item->l_data,0,size);
213 return item;
214 }
215
216 /*
217 * creat_item:
218 * Create just an item structure -- don't make any contents
219 */
220
221 struct linked_list *
222 creat_item()
223 {
224 register struct linked_list *item;
225
226 if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
227 msg("Ran out of memory for header after %d items", total);
228 item->l_next = item->l_prev = NULL;
229 return item;
230 }
231
232 char *
233 new(size)
234 int size;
235 {
236 register char *space = ALLOC(size);
237
238 if (space == NULL) {
239 sprintf(prbuf,"Rogue ran out of memory (used = %d, wanted = %d).",
240 md_memused(), size);
241 fatal(prbuf);
242 }
243 total++;
244 return space;
245 }
246