Mercurial > hg > early-roguelike
comparison arogue7/list.c @ 125:adfa37e67084
Import Advanced Rogue 7.7 from the Roguelike Restoration Project (r1490)
| author | John "Elwin" Edwards |
|---|---|
| date | Fri, 08 May 2015 15:24:40 -0400 |
| parents | |
| children | f9ef86cf22b2 |
comparison
equal
deleted
inserted
replaced
| 124:d10fc4a065ac | 125:adfa37e67084 |
|---|---|
| 1 /* | |
| 2 * list.c - Functions for dealing with linked lists of goodies | |
| 3 * | |
| 4 * Advanced Rogue | |
| 5 * Copyright (C) 1984, 1985, 1986 Michael Morgan, Ken Dalka and AT&T | |
| 6 * All rights reserved. | |
| 7 * | |
| 8 * Based on "Rogue: Exploring the Dungeons of Doom" | |
| 9 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman | |
| 10 * All rights reserved. | |
| 11 * | |
| 12 * See the file LICENSE.TXT for full copyright and licensing information. | |
| 13 */ | |
| 14 | |
| 15 /* | |
| 16 * Functions for dealing with linked lists of goodies | |
| 17 * | |
| 18 */ | |
| 19 | |
| 20 #include "curses.h" | |
| 21 #include <stdlib.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_list: | |
| 101 * Throw the whole list of room exits away | |
| 102 */ | |
| 103 | |
| 104 _r_free_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 r_discard(item); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 /* | |
| 118 * r_discard: | |
| 119 * free up an item and its room | |
| 120 */ | |
| 121 | |
| 122 r_discard(item) | |
| 123 register struct linked_list *item; | |
| 124 { | |
| 125 total -= 2; | |
| 126 FREE(DOORPTR(item)); | |
| 127 FREE(item); | |
| 128 } | |
| 129 | |
| 130 /* | |
| 131 * t_free_list: | |
| 132 * Throw the whole thing list away | |
| 133 */ | |
| 134 | |
| 135 _t_free_list(ptr) | |
| 136 register struct linked_list **ptr; | |
| 137 { | |
| 138 register struct linked_list *item; | |
| 139 | |
| 140 while (*ptr != NULL) | |
| 141 { | |
| 142 item = *ptr; | |
| 143 *ptr = next(item); | |
| 144 t_discard(item); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 /* | |
| 149 * t_discard: | |
| 150 * free up an item and its thing | |
| 151 */ | |
| 152 | |
| 153 t_discard(item) | |
| 154 register struct linked_list *item; | |
| 155 { | |
| 156 register struct thing *tp; | |
| 157 | |
| 158 total -= 2; | |
| 159 tp = THINGPTR(item); | |
| 160 if (tp->t_name != NULL) FREE(tp->t_name); | |
| 161 FREE(tp); | |
| 162 FREE(item); | |
| 163 } | |
| 164 | |
| 165 /* | |
| 166 * destroy_item: | |
| 167 * get rid of an item structure -- don't worry about contents | |
| 168 */ | |
| 169 | |
| 170 destroy_item(item) | |
| 171 register struct linked_list *item; | |
| 172 { | |
| 173 total--; | |
| 174 FREE(item); | |
| 175 } | |
| 176 | |
| 177 /* | |
| 178 * new_item | |
| 179 * get a new item with a specified size | |
| 180 */ | |
| 181 | |
| 182 struct linked_list * | |
| 183 new_item(size) | |
| 184 int size; | |
| 185 { | |
| 186 register struct linked_list *item; | |
| 187 | |
| 188 if ((item = (struct linked_list *) new(sizeof *item)) == NULL) | |
| 189 msg("Ran out of memory for header after %d items", total); | |
| 190 if ((item->l_data = new(size)) == NULL) | |
| 191 msg("Ran out of memory for data after %d items", total); | |
| 192 item->l_next = item->l_prev = NULL; | |
| 193 return item; | |
| 194 } | |
| 195 | |
| 196 /* | |
| 197 * creat_item: | |
| 198 * Create just an item structure -- don't make any contents | |
| 199 */ | |
| 200 | |
| 201 struct linked_list * | |
| 202 creat_item() | |
| 203 { | |
| 204 register struct linked_list *item; | |
| 205 | |
| 206 if ((item = (struct linked_list *) new(sizeof *item)) == NULL) | |
| 207 msg("Ran out of memory for header after %d items", total); | |
| 208 item->l_next = item->l_prev = NULL; | |
| 209 return item; | |
| 210 } | |
| 211 | |
| 212 char * | |
| 213 new(size) | |
| 214 int size; | |
| 215 { | |
| 216 register char *space = ALLOC(size); | |
| 217 static char errbuf[LINELEN]; | |
| 218 | |
| 219 if (space == NULL) { | |
| 220 sprintf(errbuf,"Rogue ran out of memory (used = %d, wanted = %d).", | |
| 221 md_memused(), size); | |
| 222 fatal(errbuf); | |
| 223 } | |
| 224 total++; | |
| 225 return space; | |
| 226 } |
