Crossfire Server, Trunk
ArtifactLoader.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 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 
13 #include "ArtifactLoader.h"
14 #include "Utils.h"
15 #include "global.h"
16 #include "artifact.h"
17 
18 bool ArtifactLoader::willLoad(const std::string &filename) {
19  return
20  Utils::endsWith(filename.c_str(), "/artifacts")
21  || Utils::endsWith(filename.c_str(), ".artifacts");
22 }
23 
24 void ArtifactLoader::load(BufferReader *reader, const std::string &filename) {
25  char *buf, *cp, *next;
26  artifact *art = NULL;
27  int value;
28  artifactlist *al;
29  archetype dummy_archetype;
30 
31  memset(&dummy_archetype, 0, sizeof(archetype));
32 
33  while ((buf = bufferreader_next_line(reader)) != NULL) {
34  if (*buf == '#')
35  continue;
36  cp = buf;
37  while (*cp == ' ') /* Skip blanks */
38  cp++;
39  if (*cp == '\0')
40  continue;
41 
42  if (!strncmp(cp, "Allowed", 7)) {
43  if (art == NULL) {
44  art = get_empty_artifact();
45  nrofartifacts++;
46  }
47 
48  cp = strchr(cp, ' ')+1;
49  while (*(cp+strlen(cp)-1) == ' ')
50  cp[strlen(cp)-1] = '\0';
51 
52  if (!strcmp(cp, "all"))
53  continue;
54 
55  do {
56  while (*cp == ' ')
57  cp++;
59  if ((next = strchr(cp, ',')) != NULL)
60  *(next++) = '\0';
61  art->allowed.push_back(add_string(cp));
62  } while ((cp = next) != NULL);
63  } else if (sscanf(cp, "chance %d", &value) && art)
64  art->chance = (uint16_t)value;
65  else if (sscanf(cp, "difficulty %d", &value) && art)
66  art->difficulty = (uint8_t)value;
67  else if (!strncmp(cp, "Object", 6) && art) {
68  art->item = (object *)calloc(1, sizeof(object));
69  if (art->item == NULL) {
70  LOG(llevError, "init_artifacts: memory allocation failure.\n");
71  abort();
72  }
73  object_reset(art->item);
74  art->item->arch = &dummy_archetype;
75  if (!load_object_from_reader(reader, art->item, MAP_STYLE, false, true))
76  LOG(llevError, "Init_Artifacts: Could not load object.\n");
77  art->item->arch = NULL;
78  art->item->name = add_string((strchr(cp, ' ')+1));
79  al = find_artifactlist(art->item->type);
80  if (al == NULL) {
82  al->type = art->item->type;
84  first_artifactlist = al;
85  }
86  al->items.push_back(art);
87  if (myTracker) {
89  }
90  art = NULL;
91  } else
92  LOG(llevError, "Unknown input in artifact file %s:%zu: %s\n", filename.c_str(), bufferreader_current_line(reader), buf);
93  }
94 
95  for (al = first_artifactlist; al != NULL; al = al->next) {
96  al->total_chance = 0;
97  for (auto art : al->items) {
98  if (!art->chance)
99  LOG(llevDebug, "Artifact with no chance: %s\n", art->item->name);
100  else
101  al->total_chance += art->chance;
102  }
103 #if 0
104  LOG(llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance);
105 #endif
106  }
107 }
give.next
def next
Definition: give.py:44
ArtifactLoader::willLoad
virtual bool willLoad(const std::string &filename) override
Definition: ArtifactLoader.cpp:18
ArtifactLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: ArtifactLoader.cpp:24
global.h
nrofartifacts
long nrofartifacts
Definition: init.cpp:116
bufferreader_current_line
size_t bufferreader_current_line(BufferReader *br)
Definition: bufferreader.cpp:140
llevError
@ llevError
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:58
object::arch
struct archetype * arch
Definition: object.h:424
artifactlist::items
std::vector< artifact * > items
Definition: artifact.h:28
get_empty_artifactlist
artifactlist * get_empty_artifactlist(void)
Definition: artifact.cpp:37
artifact::item
object * item
Definition: artifact.h:15
object_reset
void object_reset(object *op)
Definition: object.cpp:934
artifact.h
artifactlist::next
artifactlist * next
Definition: artifact.h:27
artifact::allowed
std::vector< sstring > allowed
Definition: artifact.h:18
npc_dialog.filename
filename
Definition: npc_dialog.py:99
buf
StringBuffer * buf
Definition: readable.cpp:1565
MAP_STYLE
#define MAP_STYLE
Definition: map.h:94
nrofallowedstr
long nrofallowedstr
Definition: init.cpp:117
Utils.h
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
AssetsTracker::assetDefined
virtual void assetDefined(const archetype *asset, const std::string &filename)
Definition: AssetsTracker.h:34
object::type
uint8_t type
Definition: object.h:348
artifactlist
Definition: artifact.h:24
archetype
Definition: object.h:483
object::name
sstring name
Definition: object.h:319
artifact::chance
uint16_t chance
Definition: artifact.h:16
autojail.value
value
Definition: autojail.py:6
artifactlist::total_chance
uint16_t total_chance
Definition: artifact.h:26
first_artifactlist
artifactlist * first_artifactlist
Definition: init.cpp:109
artifact::difficulty
uint8_t difficulty
Definition: artifact.h:17
ArtifactLoader.h
artifact
Definition: artifact.h:14
get_empty_artifact
artifact * get_empty_artifact(void)
Definition: artifact.cpp:55
BufferReader
Definition: bufferreader.cpp:21
find_artifactlist
artifactlist * find_artifactlist(int type)
Definition: artifact.cpp:574
artifactlist::type
uint8_t type
Definition: artifact.h:25
Utils::endsWith
static bool endsWith(const char *const str, const char *const with)
Definition: Utils.cpp:16
llevDebug
@ llevDebug
Definition: logger.h:13
ArtifactLoader::myTracker
AssetsTracker * myTracker
Definition: ArtifactLoader.h:27
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Definition: bufferreader.cpp:102
load_object_from_reader
int load_object_from_reader(BufferReader *reader, object *op, int map_flags, bool arch_init, bool artifact_init)
Definition: loader.cpp:38996