Crossfire Server, Trunk  1.75.0
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) {
88  myTracker->assetDefined(art, filename);
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 }
ArtifactLoader::willLoad
virtual bool willLoad(const std::string &filename) override
Whether this instance will process or not the specified file.
Definition: ArtifactLoader.cpp:18
ArtifactLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Load assets from the specified reader.
Definition: ArtifactLoader.cpp:24
global.h
nrofartifacts
long nrofartifacts
Only used in malloc_info().
Definition: init.cpp:116
bufferreader_current_line
size_t bufferreader_current_line(BufferReader *br)
Return the index of the last line returned by bufferreader_next_line().
Definition: bufferreader.cpp:140
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
object::arch
struct archetype * arch
Pointer to archetype.
Definition: object.h:424
artifactlist::items
std::vector< artifact * > items
Artifacts for this type.
Definition: artifact.h:28
get_empty_artifactlist
artifactlist * get_empty_artifactlist(void)
Allocate and return the pointer to an empty artifactlist structure.
Definition: artifact.cpp:37
artifact::item
object * item
Special values of the artifact.
Definition: artifact.h:15
object_reset
void object_reset(object *op)
Totally resets the specified object, without freeing associated memory.
Definition: object.cpp:919
artifact.h
artifactlist::next
artifactlist * next
Next list of artifacts.
Definition: artifact.h:27
artifact::allowed
std::vector< sstring > allowed
List of archetypes the artifact can affect.
Definition: artifact.h:18
buf
StringBuffer * buf
Definition: readable.cpp:1565
MAP_STYLE
#define MAP_STYLE
Active objects shouldn't be put on active list.
Definition: map.h:94
nrofallowedstr
long nrofallowedstr
Only used in malloc_info().
Definition: init.cpp:117
Utils.h
add_string
sstring add_string(const char *str)
This will add 'str' to the hash table.
Definition: shstr.cpp:124
AssetsTracker::assetDefined
virtual void assetDefined(const archetype *asset, const std::string &filename)
Function called when an asset is defined in a file.
Definition: AssetsTracker.h:32
object::type
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:348
artifactlist
This represents all archetypes for one particular object type.
Definition: artifact.h:24
archetype
The archetype structure is a set of rules on how to generate and manipulate objects which point to ar...
Definition: object.h:483
object::name
sstring name
The name of the object, obviously...
Definition: object.h:319
artifact::chance
uint16_t chance
Chance of the artifact to happen.
Definition: artifact.h:16
artifactlist::total_chance
uint16_t total_chance
Sum of chance for are artifacts on this list.
Definition: artifact.h:26
first_artifactlist
artifactlist * first_artifactlist
First artifact.
Definition: init.cpp:109
artifact::difficulty
uint8_t difficulty
Minimum map difficulty for the artifact to happen.
Definition: artifact.h:17
ArtifactLoader.h
artifact
This is one artifact, ie one special item.
Definition: artifact.h:14
get_empty_artifact
artifact * get_empty_artifact(void)
Allocate and return the pointer to an empty artifact structure.
Definition: artifact.cpp:55
BufferReader
Definition: bufferreader.cpp:21
find_artifactlist
artifactlist * find_artifactlist(int type)
Finds the artifact list for a certain item type.
Definition: artifact.cpp:574
artifactlist::type
uint8_t type
Object type that this list represents.
Definition: artifact.h:25
Utils::endsWith
static bool endsWith(const char *const str, const char *const with)
Checks if a string ends with another one.
Definition: Utils.cpp:16
llevDebug
@ llevDebug
Only for debugging purposes.
Definition: logger.h:13
ArtifactLoader::myTracker
AssetsTracker * myTracker
Definition: ArtifactLoader.h:27
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
load_object_from_reader
int load_object_from_reader(BufferReader *reader, object *op, int map_flags, bool arch_init, bool artifact_init)
Load an object from the specified reader, stopping when the object is complete.
Definition: loader.cpp:39005