Crossfire Server, Trunk
friend.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
18 #include "global.h"
19 
20 #include <stdlib.h>
21 
23 static std::vector<std::pair<object *, tag_t>> friends;
24 
32 void add_friendly_object(object *op) {
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  friends.push_back(std::make_pair(op, op->count));
44 }
45 
52 void remove_friendly_object(object *op) {
54  auto find = std::find_if(friends.begin(), friends.end(), [&] (auto item) { return item.first == op; });
55  if (find != friends.end()) {
56  if ((*find).second != op->count) {
57  LOG(llevError, "remove_friendly_object, tags do no match, %s, %u != %u\n",
58  op->name ? op->name : "none", op->count, (*find).second);
59  }
60  friends.erase(find);
61  }
62 }
63 
71  std::for_each(friends.begin(), friends.end(), [] (const auto item) {
72  LOG(llevError, "%s (%u)\n", item.first->name, item.second);
73  });
74 }
75 
80 void clean_friendly_list(void) {
81  int count = 0;
82 
83  auto item = friends.begin();
84  while (item != friends.end()) {
85  if (QUERY_FLAG((*item).first, FLAG_FREED)
86  || !QUERY_FLAG((*item).first, FLAG_FRIENDLY)
87  || ((*item).first->count != (*item).second)) {
88  ++count;
89  item = friends.erase(item);
90  } else {
91  ++item;
92  }
93  }
94 
95  friends.shrink_to_fit();
96  if (count)
97  LOG(llevDebug, "clean_friendly_list: Removed %d bogus links\n", count);
98 }
99 
108 int is_friendly(const object *op) {
109  return std::find_if(friends.begin(), friends.end(), [&] (auto item) { return item.first == op; }) != friends.end();
110 }
111 
117 objectlink *get_friends_of(const object *owner) {
118  objectlink *list = NULL;
119  std::for_each(friends.begin(), friends.end(), [&] (auto item) {
120  if (owner == NULL || object_get_owner(item.first) == owner) {
121  objectlink *add = get_objectlink();
122  add->id = item.second;
123  add->ob = item.first;
124  add->next = list;
125  list = add;
126  }
127  });
128  return list;
129 }
130 
135  friends.clear();
136 }
137 
143 object *get_next_friend(object *current) {
144  if (friends.empty()) {
145  return nullptr;
146  }
147  if (current == nullptr) {
148  return friends.begin()->first;
149  };
150  auto pos = std::find_if(friends.begin(), friends.end(), [&] (const auto item) { return item.first == current; });
151  if (pos == friends.end()) {
152  return nullptr;
153  }
154  pos++;
155  return pos == friends.end() ? nullptr : (*pos).first;
156 }
global.h
remove_friendly_object
void remove_friendly_object(object *op)
Definition: friend.cpp:52
llevError
@ llevError
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:58
dump_friendly_objects
void dump_friendly_objects(void)
Definition: friend.cpp:70
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
get_next_friend
object * get_next_friend(object *current)
Definition: friend.cpp:143
get_friends_of
objectlink * get_friends_of(const object *owner)
Definition: friend.cpp:117
guildoracle.list
list
Definition: guildoracle.py:87
clear_friendly_list
void clear_friendly_list(void)
Definition: friend.cpp:134
friends
static std::vector< std::pair< object *, tag_t > > friends
Definition: friend.cpp:23
is_friendly
int is_friendly(const object *op)
Definition: friend.cpp:108
clean_friendly_list
void clean_friendly_list(void)
Definition: friend.cpp:80
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
disinfect.count
int count
Definition: disinfect.py:7
FLAG_FRIENDLY
#define FLAG_FRIENDLY
Definition: define.h:246
add_friendly_object
void add_friendly_object(object *op)
Definition: friend.cpp:32
item
Definition: item.py:1
give.op
op
Definition: give.py:33
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
say.item
dictionary item
Definition: say.py:149
replace.current
current
Definition: replace.py:64
llevDebug
@ llevDebug
Definition: logger.h:13