Crossfire Server  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 }
Error, serious thing.
Definition: logger.h:11
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
void dump_races(void)
Dumps all race information to stderr.
Definition: races.cpp:81
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
int rndm(int min, int max)
Returns a number between min and max.
Definition: utils.cpp:162
int level
Definition: readable.cpp:1561
std::map< std::string, std::vector< std::string > > addToRace
Definition: races.cpp:13
Global type definitions and header inclusions.
AssetsManager * getManager()
Definition: assets.cpp:309
#define QUERY_FLAG(xyz, p)
Definition: define.h:386
Archetypes * archetypes()
Get archetypes.
Definition: AssetsManager.h:44
void finish_races()
Definition: races.cpp:98
void load_races(BufferReader *reader, const char *)
Reads the races file in the lib/ directory, then overwrites old &#39;race&#39; entries.
Definition: races.cpp:47
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
Compatibility implementations of useful nonstandard types and functions.
void free_races(void)
Frees all race-related information.
Definition: races.cpp:93
std::map< std::string, std::vector< object * > > races
Definition: races.cpp:14
Only for debugging purposes.
Definition: logger.h:13
T * find(const Key &name)
Get a named asset if it exists.
#define FLAG_MONSTER
Will attack players.
Definition: define.h:232
C function wrappers to interact with assets.
StringBuffer * buf
Definition: readable.cpp:1563
char * bufferreader_next_line(BufferReader *br)
Return the next line in the buffer, as separated by a newline.