| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "global.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
void add_friendly_object(object *op) { |
| 31 |
objectlink *ol; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
if (is_friendly(op)) { |
| 39 |
LOG(llevError, "add_friendly_object: Trying to add object already on list (%s)\n", op->name); |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
ol = first_friendly_object; |
| 44 |
first_friendly_object = get_objectlink(); |
| 45 |
first_friendly_object->ob = op; |
| 46 |
first_friendly_object->id = op->count; |
| 47 |
first_friendly_object->next = ol; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
void remove_friendly_object(object *op) { |
| 57 |
objectlink *this; |
| 58 |
|
| 59 |
CLEAR_FLAG(op, FLAG_FRIENDLY); |
| 60 |
|
| 61 |
if (!first_friendly_object) { |
| 62 |
LOG(llevError, "remove_friendly_object called with empty friendly list, remove ob=%s\n", op->name); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (first_friendly_object->ob == op) { |
| 69 |
this = first_friendly_object; |
| 70 |
first_friendly_object = this->next; |
| 71 |
free(this); |
| 72 |
} else { |
| 73 |
objectlink *prev = first_friendly_object; |
| 74 |
|
| 75 |
for (this = first_friendly_object->next; this != NULL; this = this->next) { |
| 76 |
if (this->ob == op) |
| 77 |
break; |
| 78 |
prev = this; |
| 79 |
} |
| 80 |
if (this) { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if (this->id != op->count) { |
| 85 |
LOG(llevError, "remove_friendly_object, tags do no match, %s, %u != %u\n", |
| 86 |
op->name ? op->name : "none", op->count, this->id); |
| 87 |
} |
| 88 |
prev->next = this->next; |
| 89 |
free(this); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void dump_friendly_objects(void) { |
| 101 |
objectlink *ol; |
| 102 |
|
| 103 |
for (ol = first_friendly_object; ol != NULL; ol = ol->next) |
| 104 |
LOG(llevError, "%s (%u)\n", ol->ob->name, ol->ob->count); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
void clean_friendly_list(void) { |
| 112 |
objectlink *this, *prev = NULL, *next; |
| 113 |
int count = 0; |
| 114 |
|
| 115 |
for (this = first_friendly_object; this != NULL; this = next) { |
| 116 |
next = this->next; |
| 117 |
if (QUERY_FLAG(this->ob, FLAG_FREED) |
| 118 |
|| !QUERY_FLAG(this->ob, FLAG_FRIENDLY) |
| 119 |
|| (this->id != this->ob->count)) { |
| 120 |
if (prev) { |
| 121 |
prev->next = this->next; |
| 122 |
} else { |
| 123 |
first_friendly_object = this->next; |
| 124 |
} |
| 125 |
count++; |
| 126 |
free(this); |
| 127 |
|
| 128 |
} else |
| 129 |
prev = this; |
| 130 |
} |
| 131 |
if (count) |
| 132 |
LOG(llevDebug, "clean_friendly_list: Removed %d bogus links\n", count); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
int is_friendly(const object *op) { |
| 145 |
objectlink *ol; |
| 146 |
|
| 147 |
for (ol = first_friendly_object; ol != NULL; ol = ol->next) |
| 148 |
if (ol->ob == op) |
| 149 |
return 1; |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |