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