Crossfire Server, Branches 1.12  R18729
friend.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_friend_c =
3  * "$Id: friend.c 11578 2009-02-23 22:02:27Z lalo $";
4  */
5 
6 /*
7  CrossFire, A Multiplayer game for X-windows
8 
9  Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10  Copyright (C) 1992 Frank Tore Johansen
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26  The authors can be reached via e-mail at crossfire-devel@real-time.com
27 */
28 
34 #include <global.h>
35 
43 void add_friendly_object(object *op) {
44  objectlink *ol;
45 
46  /* Add some error checking. This shouldn't happen, but the friendly
47  * object list usually isn't very long, and remove_friendly_object
48  * won't remove it either. Plus, it is easier to put a breakpoint in
49  * the debugger here and see where the problem is happening.
50  */
51  if (is_friendly(op)) {
52  LOG(llevError, "add_friendly_object: Trying to add object already on list (%s)\n", op->name);
53  return;
54  }
55 
61 }
62 
69 void remove_friendly_object(object *op) {
70  objectlink *this;
71 
73 
74  if (!first_friendly_object) {
75  LOG(llevError, "remove_friendly_object called with empty friendly list, remove ob=%s\n", op->name);
76  return;
77  }
78  /* if the first object happens to be the one, processing is pretty
79  * easy.
80  */
81  if (first_friendly_object->ob == op) {
82  this = first_friendly_object;
83  first_friendly_object = this->next;
84  free(this);
85  } else {
87 
88  for (this = first_friendly_object->next; this != NULL; this = this->next) {
89  if (this->ob == op)
90  break;
91  prev = this;
92  }
93  if (this) {
94  /* This should not happen. But if it does, presumably the
95  * call to remove it is still valid.
96  */
97  if (this->id != op->count) {
98  LOG(llevError, "remove_friendly_object, tags do no match, %s, %d != %d\n",
99  op->name ? op->name : "none", op->count, this->id);
100  }
101  prev->next = this->next;
102  free(this);
103  }
104  }
105 }
106 
114  objectlink *ol;
115 
116  for (ol = first_friendly_object; ol != NULL; ol = ol->next)
117  LOG(llevError, "%s (%d)\n", ol->ob->name, ol->ob->count);
118 }
119 
125  objectlink *this, *prev = NULL, *next;
126  int count = 0;
127 
128  for (this = first_friendly_object; this != NULL; this = next) {
129  next = this->next;
130  if (QUERY_FLAG(this->ob, FLAG_FREED)
131  || !QUERY_FLAG(this->ob, FLAG_FRIENDLY)
132  || (this->id != this->ob->count)) {
133  if (prev) {
134  prev->next = this->next;
135  } else {
136  first_friendly_object = this->next;
137  }
138  count++;
139  free(this);
140  /* If we removed the object, then prev is still valid. */
141  } else
142  prev = this;
143  }
144  if (count)
145  LOG(llevDebug, "clean_friendly_list: Removed %d bogus links\n", count);
146 }
147 
157 int is_friendly(const object *op) {
158  objectlink *ol;
159 
160  for (ol = first_friendly_object; ol != NULL; ol = ol->next)
161  if (ol->ob == op)
162  return 1;
163 
164  return 0;
165 }
Definition: object.h:298
EXTERN objectlink * first_friendly_object
Definition: global.h:196
#define FLAG_FRIENDLY
Definition: define.h:542
void remove_friendly_object(object *op)
Definition: friend.c:69
void clean_friendly_list(void)
Definition: friend.c:124
tag_t id
Definition: object.h:301
object * ob
Definition: object.h:299
int is_friendly(const object *op)
Definition: friend.c:157
void add_friendly_object(object *op)
Definition: friend.c:43
const char * name
Definition: object.h:167
#define QUERY_FLAG(xyz, p)
Definition: define.h:514
#define CLEAR_FLAG(xyz, p)
Definition: define.h:512
tag_t count
Definition: object.h:157
struct oblnk * next
Definition: object.h:300
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:63
void dump_friendly_objects(void)
Definition: friend.c:113
#define FLAG_FREED
Definition: define.h:529