Debian Codegraph @

common/friend.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file friend.c
16 * Functions related to relationship management.
17 */
18
19 #include "global.h"
20
21 #include <stdlib.h>
22
23 /**
24 * Add a new friendly object to the linked list of friendly objects.
25 * Will log an error if the object is already on that list.
26 *
27 * @param op
28 * object to add to the list.
29 */
30 void add_friendly_object(object *op) {
31 objectlink *ol;
32
33 /* Add some error checking. This shouldn't happen, but the friendly
34 * object list usually isn't very long, and remove_friendly_object
35 * won't remove it either. Plus, it is easier to put a breakpoint in
36 * the debugger here and see where the problem is happening.
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 * Removes the specified object from the linked list of friendly objects.
52 *
53 * @param op
54 * object to remove from list.
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 /* if the first object happens to be the one, processing is pretty
66 * easy.
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 /* This should not happen. But if it does, presumably the
82 * call to remove it is still valid.
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 * Dumps all friendly objects. Invoked in DM-mode with dumpfriendlyobjects command.
96 *
97 * @note
98 * logs at the error level.
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 * It traverses the friendly list removing objects that should not be here
109 * (ie, do not have friendly flag set, freed, etc)
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 /* If we removed the object, then prev is still valid. */
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 * Checks if the given object is already in the friendly list or not
137 * Lauwenmark - 31/07/05
138 *
139 * @param op
140 * item to check
141 * @return
142 * 1 if on friendly list, 0 else
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 }