Crossfire Server, Trunk  R20513
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 
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;
70  first_friendly_object = this->next;
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 }
Error, serious thing.
Definition: logger.h:11
Used to link together several objects.
Definition: object.h:442
EXTERN objectlink * first_friendly_object
Objects monsters will go after.
Definition: global.h:123
#define FLAG_FRIENDLY
Will help players.
Definition: define.h:246
void remove_friendly_object(object *op)
Removes the specified object from the linked list of friendly objects.
Definition: friend.c:56
void clean_friendly_list(void)
It traverses the friendly list removing objects that should not be here (ie, do not have friendly fla...
Definition: friend.c:111
Global type definitions and header inclusions.
tag_t id
ob&#39;s tag, in case it is removed.
Definition: object.h:445
object * ob
Item to link to.
Definition: object.h:443
int is_friendly(const object *op)
Checks if the given object is already in the friendly list or not Lauwenmark - 31/07/05.
Definition: friend.c:144
void add_friendly_object(object *op)
Add a new friendly object to the linked list of friendly objects.
Definition: friend.c:30
const char * name
The name of the object, obviously...
Definition: object.h:311
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
tag_t count
Unique object number for this object.
Definition: object.h:299
struct oblnk * next
Next item to link to.
Definition: object.h:444
Only for debugging purposes.
Definition: logger.h:13
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.c:51
void dump_friendly_objects(void)
Dumps all friendly objects.
Definition: friend.c:100
#define FLAG_FREED
Object is in the list of free objects.
Definition: define.h:233