Crossfire Server, Trunk
friend.c
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
19 #include "global.h"
20 
21 #include <stdlib.h>
22 
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 
46  first_friendly_object->id = op->count;
48 }
49 
56 void remove_friendly_object(object *op) {
57  objectlink *this;
58 
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;
71  free(this);
72  } else {
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 
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 
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 
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 }
give.next
def next
Definition: give.py:44
global.h
llevError
@ llevError
Definition: logger.h:11
obj::count
tag_t count
Definition: object.h:302
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
clean_friendly_list
void clean_friendly_list(void)
Definition: friend.c:111
guildjoin.ob
ob
Definition: guildjoin.py:42
is_friendly
int is_friendly(const object *op)
Definition: friend.c:144
remove_friendly_object
void remove_friendly_object(object *op)
Definition: friend.c:56
first_friendly_object
EXTERN objectlink * first_friendly_object
Definition: global.h:119
oblnk::next
struct oblnk * next
Definition: object.h:449
obj::name
sstring name
Definition: object.h:314
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
disinfect.count
int count
Definition: disinfect.py:7
oblnk::id
tag_t id
Definition: object.h:450
dump_friendly_objects
void dump_friendly_objects(void)
Definition: friend.c:100
FLAG_FRIENDLY
#define FLAG_FRIENDLY
Definition: define.h:246
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
give.op
op
Definition: give.py:33
oblnk::ob
object * ob
Definition: object.h:448
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
add_friendly_object
void add_friendly_object(object *op)
Definition: friend.c:30
oblnk
Definition: object.h:447
llevDebug
@ llevDebug
Definition: logger.h:13