Crossfire Server, Trunk  1.75.0
races.cpp
Go to the documentation of this file.
1 #include "global.h"
2 #include "compat.h"
3 #include "string.h"
4 #include "malloc.h"
5 
6 #include <map>
7 
8 #include "sproto.h"
9 #include "assets.h"
10 #include "AssetsManager.h"
11 #include "Archetypes.h"
12 
13 std::map<std::string, std::vector<std::string> > addToRace;
14 std::map<std::string, std::vector<object *> > races;
15 
22 object *races_get_random_monster(const char *race, int level) {
23  auto r = races.find(race);
24  if (r == races.end()) {
25  LOG(llevError, "races_get_random_monster: requested non-existent aligned race %s!\n", race);
26  return NULL;
27  }
28 
29  std::vector<object *> valid;
30  for (auto it = (*r).second.begin(); it != (*r).second.end(); it++) {
31  if ((*it)->level <= level) {
32  valid.push_back(*it);
33  }
34  }
35  if (valid.empty()) {
36  return NULL;
37  }
38  return valid[rndm(0, valid.size() - 1)];
39 }
40 
47 void load_races(BufferReader *reader, const char *) {
48  char race[MAX_BUF], *buf, *cp, variable[MAX_BUF];
49 
50  while ((buf = bufferreader_next_line(reader)) != NULL) {
51  if (*buf == '#')
52  continue;
53  cp = buf;
54  while (*cp == ' ') {
55  cp++;
56  }
57  if (sscanf(cp, "RACE %s", variable)) { /* set new race value */
58  strcpy(race, variable);
59  } else {
60  char *cp1;
61 
62  /* Take out beginning spaces */
63  for (cp1 = cp; *cp1 == ' '; cp1++)
64  ;
65  /* Remove trailing spaces */
66  for (cp1 = cp+strlen(cp)-1; *cp1 == ' '; cp1--) {
67  *cp1 = '\0';
68  if (cp == cp1)
69  break;
70  }
71 
72  addToRace[race].push_back(cp);
73  }
74  }
75  LOG(llevDebug, "loaded races\n");
76 }
77 
81 void dump_races(void) {
82  for (auto race = races.cbegin(); race != races.cend(); race++) {
83  fprintf(stderr, "\nRACE %s:\t", (*race).first.c_str());
84  for (auto mon = (*race).second.cbegin(); mon != (*race).second.cend(); mon++) {
85  fprintf(stderr, "%s (%d), ", (*mon)->arch->name, (*mon)->level);
86  }
87  }
88 }
89 
93 void free_races(void) {
94  races.clear();
95  LOG(llevDebug, "Freeing race information.\n");
96 }
97 
98 void finish_races() {
99  for (const auto& add : addToRace) {
100  for (const auto& name : add.second) {
101  auto mon = getManager()->archetypes()->find(name);
102  if (mon && QUERY_FLAG(&mon->clone, FLAG_MONSTER)) {
103  races[add.first].push_back(&mon->clone);
104  } else {
105  LOG(llevError, "races: %s %s\n", name.c_str(), mon ? "is not a monster" : "does not exist");
106  }
107  }
108  }
109  addToRace.clear();
110 }
global.h
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
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
AssetsManager.h
races_get_random_monster
object * races_get_random_monster(const char *race, int level)
Get a random monster of specified race and level at most the specified one.
Definition: races.cpp:22
addToRace
std::map< std::string, std::vector< std::string > > addToRace
Definition: races.cpp:13
rndm
int rndm(int min, int max)
Returns a number between min and max.
Definition: utils.cpp:162
buf
StringBuffer * buf
Definition: readable.cpp:1565
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
AssetsCollection::find
T * find(const Key &name)
Get a named asset if it exists.
Definition: AssetsCollection.h:108
name
Plugin animator file specs[Config] name
Definition: animfiles.txt:4
compat.h
finish_races
void finish_races()
Definition: races.cpp:98
sproto.h
FLAG_MONSTER
#define FLAG_MONSTER
Will attack players.
Definition: define.h:245
AssetsManager::archetypes
Archetypes * archetypes()
Get archetypes.
Definition: AssetsManager.h:44
MAX_BUF
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
races
std::map< std::string, std::vector< object * > > races
Definition: races.cpp:14
load_races
void load_races(BufferReader *reader, const char *)
Reads the races file in the lib/ directory, then overwrites old 'race' entries.
Definition: races.cpp:47
free_races
void free_races(void)
Frees all race-related information.
Definition: races.cpp:93
assets.h
Archetypes.h
level
int level
Definition: readable.cpp:1563
it
if you malloc the data for the make sure to free it when done There is also the newclient h file which is shared between the client and server This file contains the definition of the as well as many defined values for constants of varying you will need to grab these constant values for yourself Many of the constants in this file are used in the protocol to denote types Image Caching ~ Image caching has been implemented on the with necessary server support to handle it This section will briefly describe how image caching works on the protocol as well as how the current client does it the client checks for an option denoting the image caching is desired If we initialize all the images to a default value this means we don t need to put special checks into the drawing code to see if we have an image we just draw the default we know what filename to store it as we request the server to do image caching This is done by or ing the cache directive to the image mode we want C when the server finds an image number that it has not send to the it sends us a name command information us the number to name and there is no space between that the and the name Such formating is difficult but the above example illustrates the data is sent The client then checks for the existence of the image locally It is up to the client to organize images and then splits them into sub directories based on the first letters in the above the file would be crossfire images CS CSword If the client does not have the image or otherwise needs a copy from the it then requests it
Definition: protocol.txt:2164
BufferReader
Definition: bufferreader.cpp:21
llevDebug
@ llevDebug
Only for debugging purposes.
Definition: logger.h:13
dump_races
void dump_races(void)
Dumps all race information to stderr.
Definition: races.cpp:81
variable
*envar *is the environment variable
Definition: server-directories.txt:9
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Return the next line in the buffer, as separated by a newline.
Definition: bufferreader.cpp:102