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 #include <vector>
22 #include <algorithm>
23 
25 static std::vector<std::pair<object *, tag_t>> friends;
26 
34 void add_friendly_object(object *op) {
35  /* Add some error checking. This shouldn't happen, but the friendly
36  * object list usually isn't very long, and remove_friendly_object
37  * won't remove it either. Plus, it is easier to put a breakpoint in
38  * the debugger here and see where the problem is happening.
39  */
40  if (is_friendly(op)) {
41  LOG(llevError, "add_friendly_object: Trying to add object already on list (%s)\n", op->name);
42  return;
43  }
44 
45  friends.push_back(std::make_pair(op, op->count));
46 }
47 
54 void remove_friendly_object(object *op) {
56  auto find = std::find_if(friends.begin(), friends.end(), [&] (auto item) { return item.first == op; });
57  if (find != friends.end()) {
58  if ((*find).second != op->count) {
59  LOG(llevError, "remove_friendly_object, tags do no match, %s, %u != %u\n",
60  op->name ? op->name : "none", op->count, (*find).second);
61  }
62  friends.erase(find);
63  }
64 }
65 
73  std::for_each(friends.begin(), friends.end(), [] (const auto item) {
74  LOG(llevError, "%s (%u)\n", item.first->name, item.second);
75  });
76 }
77 
82 void clean_friendly_list(void) {
83  int count = 0;
84 
85  auto item = friends.begin();
86  while (item != friends.end()) {
87  if (QUERY_FLAG((*item).first, FLAG_FREED)
88  || !QUERY_FLAG((*item).first, FLAG_FRIENDLY)
89  || ((*item).first->count != (*item).second)) {
90  ++count;
91  item = friends.erase(item);
92  } else {
93  ++item;
94  }
95  }
96 
97  friends.shrink_to_fit();
98  if (count)
99  LOG(llevDebug, "clean_friendly_list: Removed %d bogus links\n", count);
100 }
101 
110 int is_friendly(const object *op) {
111  return std::find_if(friends.begin(), friends.end(), [&] (auto item) { return item.first == op; }) != friends.end();
112 }
113 
119 objectlink *get_friends_of(const object *owner) {
120  objectlink *list = NULL;
121  std::for_each(friends.begin(), friends.end(), [&] (auto item) {
122  if (owner == NULL || object_get_owner(item.first) == owner) {
123  objectlink *add = get_objectlink();
124  add->id = item.second;
125  add->ob = item.first;
126  add->next = list;
127  list = add;
128  }
129  });
130  return list;
131 }
132 
137  friends.clear();
138 }
139 
145 object *get_next_friend(object *current) {
146  if (friends.empty()) {
147  return nullptr;
148  }
149  if (current == nullptr) {
150  return friends.begin()->first;
151  };
152  auto pos = std::find_if(friends.begin(), friends.end(), [&] (const auto item) { return item.first == current; });
153  if (pos == friends.end()) {
154  return nullptr;
155  }
156  pos++;
157  return pos == friends.end() ? nullptr : (*pos).first;
158 }
global.h
remove_friendly_object
void remove_friendly_object(object *op)
Definition: friend.cpp:54
llevError
@ llevError
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:51
dump_friendly_objects
void dump_friendly_objects(void)
Definition: friend.cpp:72
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
get_next_friend
object * get_next_friend(object *current)
Definition: friend.cpp:145
get_friends_of
objectlink * get_friends_of(const object *owner)
Definition: friend.cpp:119
guildoracle.list
list
Definition: guildoracle.py:87
clear_friendly_list
void clear_friendly_list(void)
Definition: friend.cpp:136
friends
static std::vector< std::pair< object *, tag_t > > friends
Definition: friend.cpp:25
is_friendly
int is_friendly(const object *op)
Definition: friend.cpp:110
clean_friendly_list
void clean_friendly_list(void)
Definition: friend.cpp:82
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:34
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
oblnk
Definition: object.h:449
replace.current
current
Definition: replace.py:64
llevDebug
@ llevDebug
Definition: logger.h:13