comparison xrogue/list.c @ 142:6b5fbd7c3ece

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