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 extern "C" {
16 #include "global.h"
17 #include "artifact.h"
18 }
19 
22 bool ArtifactLoader::willLoad(const std::string &filename) {
23  return
24  Utils::endsWith(filename, "/artifacts")
25  || Utils::endsWith(filename, ".artifacts");
26 }
27 
28 void ArtifactLoader::load(BufferReader *reader, const std::string &filename) {
29  char *buf, *cp, *next;
30  artifact *art = NULL;
32  int value;
33  artifactlist *al;
34  archetype dummy_archetype;
35 
36  memset(&dummy_archetype, 0, sizeof(archetype));
37 
38  artifact_init = 1;
39 
40  while ((buf = bufferreader_next_line(reader)) != NULL) {
41  if (*buf == '#')
42  continue;
43  cp = buf;
44  while (*cp == ' ') /* Skip blanks */
45  cp++;
46  if (*cp == '\0')
47  continue;
48 
49  if (!strncmp(cp, "Allowed", 7)) {
50  if (art == NULL) {
51  art = get_empty_artifact();
52  nrofartifacts++;
53  }
54 
55  cp = strchr(cp, ' ')+1;
56  while (*(cp+strlen(cp)-1) == ' ')
57  cp[strlen(cp)-1] = '\0';
58 
59  if (!strcmp(cp, "all"))
60  continue;
61 
62  do {
63  while (*cp == ' ')
64  cp++;
66  if ((next = strchr(cp, ',')) != NULL)
67  *(next++) = '\0';
68  tmp = (linked_char *)malloc(sizeof(linked_char));
69  tmp->name = add_string(cp);
70  tmp->next = art->allowed;
71  art->allowed = tmp;
72  art->allowed_size++;
73  } while ((cp = next) != NULL);
74  } else if (sscanf(cp, "chance %d", &value))
75  art->chance = (uint16_t)value;
76  else if (sscanf(cp, "difficulty %d", &value))
77  art->difficulty = (uint8_t)value;
78  else if (!strncmp(cp, "Object", 6)) {
79  art->item = (object *)calloc(1, sizeof(object));
80  if (art->item == NULL) {
81  LOG(llevError, "init_artifacts: memory allocation failure.\n");
82  abort();
83  }
84  object_reset(art->item);
85  art->item->arch = &dummy_archetype;
86  if (!load_object_from_reader(reader, art->item, MAP_STYLE))
87  LOG(llevError, "Init_Artifacts: Could not load object.\n");
88  art->item->arch = NULL;
89  art->item->name = add_string((strchr(cp, ' ')+1));
90  al = find_artifactlist(art->item->type);
91  if (al == NULL) {
93  al->type = art->item->type;
95  first_artifactlist = al;
96  }
97  art->next = al->items;
98  al->items = art;
99  if (myTracker) {
101  }
102  art = NULL;
103  } else
104  LOG(llevError, "Unknown input in artifact file %s:%d: %s\n", filename.c_str(), bufferreader_current_line(reader), buf);
105  }
106 
107  for (al = first_artifactlist; al != NULL; al = al->next) {
108  al->total_chance = 0;
109  for (art = al->items; art != NULL; art = art->next) {
110  if (!art->chance)
111  LOG(llevDebug, "Artifact with no chance: %s\n", art->item->name);
112  else
113  al->total_chance += art->chance;
114  }
115 #if 0
116  LOG(llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance);
117 #endif
118  }
119 
120  artifact_init = 0;
121 }
give.next
def next
Definition: give.py:44
ArtifactLoader::willLoad
virtual bool willLoad(const std::string &filename) override
Definition: ArtifactLoader.cpp:22
nrofallowedstr
EXTERN long nrofallowedstr
Definition: global.h:138
ArtifactLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: ArtifactLoader.cpp:28
global.h
get_empty_artifactlist
artifactlist * get_empty_artifactlist(void)
Definition: artifact.c:37
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
get_empty_artifact
artifact * get_empty_artifact(void)
Definition: artifact.c:56
llevError
@ llevError
Definition: logger.h:11
object_reset
void object_reset(object *op)
Definition: object.c:938
artifactliststruct::total_chance
uint16_t total_chance
Definition: artifact.h:28
load_object_from_reader
int load_object_from_reader(BufferReader *reader, object *op, int map_flags)
Definition: loader.c:5255
artifact_init
int artifact_init
Definition: ArtifactLoader.cpp:20
first_artifactlist
EXTERN artifactlist * first_artifactlist
Definition: global.h:118
artifact.h
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Definition: bufferreader.c:102
bufferreader_current_line
size_t bufferreader_current_line(BufferReader *br)
Definition: bufferreader.c:140
artifactliststruct::items
struct artifactstruct * items
Definition: artifact.h:30
Ice.tmp
int tmp
Definition: Ice.py:207
npc_dialog.filename
filename
Definition: npc_dialog.py:99
MAP_STYLE
#define MAP_STYLE
Definition: map.h:99
linked_char
Definition: global.h:86
archt
Definition: object.h:469
artifactstruct::chance
uint16_t chance
Definition: artifact.h:16
obj::name
sstring name
Definition: object.h:314
find_artifactlist
artifactlist * find_artifactlist(int type)
Definition: artifact.c:575
Utils.h
artifactstruct::next
struct artifactstruct * next
Definition: artifact.h:18
AssetsTracker::assetDefined
virtual void assetDefined(const archetype *asset, const std::string &filename)
Definition: AssetsTracker.h:36
artifactliststruct::next
struct artifactliststruct * next
Definition: artifact.h:29
artifactstruct::difficulty
uint8_t difficulty
Definition: artifact.h:17
artifactstruct::allowed_size
int allowed_size
Definition: artifact.h:20
artifactliststruct
Definition: artifact.h:26
artifactstruct
Definition: artifact.h:14
obj::arch
struct archt * arch
Definition: object.h:417
obj::type
uint8_t type
Definition: object.h:343
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
autojail.value
value
Definition: autojail.py:6
Utils::endsWith
static bool endsWith(const std::string &str, const std::string &with)
Definition: Utils.cpp:16
buf
StringBuffer * buf
Definition: readable.c:1610
artifactstruct::item
object * item
Definition: artifact.h:15
nrofartifacts
EXTERN long nrofartifacts
Definition: global.h:137
ArtifactLoader.h
artifactliststruct::type
uint8_t type
Definition: artifact.h:27
BufferReader
Definition: bufferreader.c:21
llevDebug
@ llevDebug
Definition: logger.h:13
artifactstruct::allowed
linked_char * allowed
Definition: artifact.h:19
ArtifactLoader::myTracker
AssetsTracker * myTracker
Definition: ArtifactLoader.h:27