Crossfire Server, Trunk  1.75.0
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)
Removes the specified object from the linked list of friendly objects.
Definition: friend.cpp:52
llevError
@ llevError
Error, serious thing.
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
dump_friendly_objects
void dump_friendly_objects(void)
Dumps all friendly objects.
Definition: friend.cpp:70
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
get_next_friend
object * get_next_friend(object *current)
Get the next object on the friendly list.
Definition: friend.cpp:143
get_friends_of
objectlink * get_friends_of(const object *owner)
Get a list of friendly objects for the specified owner.
Definition: friend.cpp:117
object::count
tag_t count
Unique object number for this object.
Definition: object.h:307
clear_friendly_list
void clear_friendly_list(void)
Totally clear the friendly list.
Definition: friend.cpp:134
friends
static std::vector< std::pair< object *, tag_t > > friends
List of all friendly objects, object and its count.
Definition: friend.cpp:23
is_friendly
int is_friendly(const object *op)
Checks if the given object is already in the friendly list or not.
Definition: friend.cpp:108
clean_friendly_list
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.cpp:80
FLAG_FREED
#define FLAG_FREED
Object is in the list of free objects.
Definition: define.h:233
FLAG_FRIENDLY
#define FLAG_FRIENDLY
Will help players.
Definition: define.h:246
object::name
sstring name
The name of the object, obviously...
Definition: object.h:319
add_friendly_object
void add_friendly_object(object *op)
Add a new friendly object to the list of friendly objects.
Definition: friend.cpp:32
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
list
How to Install a Crossfire Server on you must install a python script engine on your computer Python is the default script engine of Crossfire You can find the python engine you have only to install them The VisualC Crossfire settings are for but you habe then to change the pathes in the VC settings Go in Settings C and Settings Link and change the optional include and libs path to the new python installation path o except the maps ! You must download a map package and install them the share folder Its must look like doubleclick on crossfire32 dsw There are projects in your libcross lib and plugin_python You need to compile all Easiest way is to select the plugin_python ReleaseLog as active this will compile all others too Then in Visual C press< F7 > to compile If you don t have an appropriate compiler you can try to get the the VC copies the crossfire32 exe in the crossfire folder and the plugin_python dll in the crossfire share plugins folder we will remove it when we get time for it o Last showing lots of weird write to the Crossfire mailing list
Definition: INSTALL_WIN32.txt:50
llevDebug
@ llevDebug
Only for debugging purposes.
Definition: logger.h:13