Crossfire Server, Trunk
races.cpp
Go to the documentation of this file.
1 extern "C" {
2 #include "global.h"
3 #include "compat.h"
4 #include "string.h"
5 #include "malloc.h"
6 }
7 #include <string>
8 #include <map>
9 #include <vector>
10 
11 #include "sproto.h"
12 #include "assets.h"
13 #include "AssetsManager.h"
14 #include "Archetypes.h"
15 
16 std::map<std::string, std::vector<std::string> > addToRace;
17 std::map<std::string, std::vector<object *> > races;
18 
25 object *races_get_random_monster(const char *race, int level) {
26  auto r = races.find(race);
27  if (r == races.end()) {
28  LOG(llevError, "races_get_random_monster: requested non-existent aligned race %s!\n", race);
29  return NULL;
30  }
31 
32  std::vector<object *> valid;
33  for (auto it = (*r).second.begin(); it != (*r).second.end(); it++) {
34  if ((*it)->level <= level) {
35  valid.push_back(*it);
36  }
37  }
38  if (valid.empty()) {
39  return NULL;
40  }
41  return valid[rndm(0, valid.size() - 1)];
42 }
43 
50 void load_races(BufferReader *reader, const char *) {
51  char race[MAX_BUF], *buf, *cp, variable[MAX_BUF];
52 
53  while ((buf = bufferreader_next_line(reader)) != NULL) {
54  if (*buf == '#')
55  continue;
56  cp = buf;
57  while (*cp == ' ') {
58  cp++;
59  }
60  if (sscanf(cp, "RACE %s", variable)) { /* set new race value */
61  strcpy(race, variable);
62  } else {
63  char *cp1;
64 
65  /* Take out beginning spaces */
66  for (cp1 = cp; *cp1 == ' '; cp1++)
67  ;
68  /* Remove trailing spaces */
69  for (cp1 = cp+strlen(cp)-1; *cp1 == ' '; cp1--) {
70  *cp1 = '\0';
71  if (cp == cp1)
72  break;
73  }
74 
75  addToRace[race].push_back(cp);
76  }
77  }
78  LOG(llevDebug, "loaded races\n");
79 }
80 
84 void dump_races(void) {
85  for (auto race = races.cbegin(); race != races.cend(); race++) {
86  fprintf(stderr, "\nRACE %s:\t", (*race).first.c_str());
87  for (auto mon = (*race).second.cbegin(); mon != (*race).second.cend(); mon++) {
88  fprintf(stderr, "%s (%d), ", (*mon)->arch->name, (*mon)->level);
89  }
90  }
91 }
92 
96 void free_races(void) {
97  races.clear();
98  LOG(llevDebug, "Freeing race information.\n");
99 }
100 
101 void finish_races() {
102  for (const auto& add : addToRace) {
103  for (const auto& name : add.second) {
104  auto mon = getManager()->archetypes()->find(name);
105  if (mon && QUERY_FLAG(&mon->clone, FLAG_MONSTER)) {
106  races[add.first].push_back(&mon->clone);
107  } else {
108  LOG(llevError, "races: %s %s\n", name.c_str(), mon ? "is not a monster" : "does not exist");
109  }
110  }
111  }
112  addToRace.clear();
113 }
global.h
llevError
@ llevError
Definition: logger.h:11
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)
Definition: races.cpp:25
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Definition: bufferreader.c:102
addToRace
std::map< std::string, std::vector< std::string > > addToRace
Definition: races.cpp:16
getManager
AssetsManager * getManager()
Definition: assets.cpp:329
AssetsCollection::find
T * find(const Key &name)
Definition: AssetsCollection.h:85
mon
object * mon
Definition: comet_perf.c:75
compat.h
finish_races
void finish_races()
Definition: races.cpp:101
rndm
int rndm(int min, int max)
Definition: utils.c:162
sproto.h
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
MAX_BUF
#define MAX_BUF
Definition: define.h:35
races
std::map< std::string, std::vector< object * > > races
Definition: races.cpp:17
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
load_races
void load_races(BufferReader *reader, const char *)
Definition: races.cpp:50
free_races
void free_races(void)
Definition: races.cpp:96
assets.h
Archetypes.h
buf
StringBuffer * buf
Definition: readable.c:1606
BufferReader
Definition: bufferreader.c:21
ring_occidental_mages.r
r
Definition: ring_occidental_mages.py:6
llevDebug
@ llevDebug
Definition: logger.h:13
dump_races
void dump_races(void)
Definition: races.cpp:84
give.name
name
Definition: give.py:27
level
Definition: level.py:1