Crossfire Server, Trunk
TreasureLoader.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2020 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 "TreasureLoader.h"
14 #include "Archetypes.h"
15 #include "Treasures.h"
16 #include "Utils.h"
17 
18 extern "C" {
19 #include "string.h"
20 #include "global.h"
21 #include "compat.h"
22 }
23 
25  : m_treasures(treasures), m_archetypes(archetypes) {
26 }
27 
40  treasure *t = (treasure *)calloc(1, sizeof(treasure));
41  if (t == NULL)
43  t->item = NULL;
44  t->name = NULL;
45  t->next = NULL;
46  t->next_yes = NULL;
47  t->next_no = NULL;
48  t->chance = 100;
49  t->magic = 0;
50  t->nrof = 0;
51  return t;
52 }
53 
54 extern size_t nroftreasures;
55 
68  char *buf, *cp, variable[MAX_BUF];
70  int value;
71 
72  nroftreasures++;
73  while ((buf = bufferreader_next_line(reader)) != NULL) {
74 
75  if (*buf == '#')
76  continue;
77  cp = buf;
78  while (isspace(*cp)) /* Skip blanks */
79  cp++;
80 
81  if (sscanf(cp, "arch %s", variable)) {
82  if (t->item) {
83  LOG(llevError, "treasure: duplicate 'arch' in %s:%d\n", filename.c_str(), bufferreader_current_line(reader));
84  }
85  t->item = m_archetypes->get(variable);
86  } else if (sscanf(cp, "list %s", variable)) {
87  if (t->name) {
88  LOG(llevError, "treasure: duplicate 'name' in %s:%d\n", filename.c_str(), bufferreader_current_line(reader));
89  free_string(t->name);
90  }
91  t->name = add_string(variable);
92  } else if (sscanf(cp, "change_name %s", variable))
93  t->change_arch.name = add_string(variable);
94  else if (sscanf(cp, "change_title %s", variable))
95  t->change_arch.title = add_string(variable);
96  else if (sscanf(cp, "change_slaying %s", variable))
97  t->change_arch.slaying = add_string(variable);
98  else if (sscanf(cp, "chance %d", &value))
99  t->chance = (uint8_t)value;
100  else if (sscanf(cp, "nrof %d", &value))
101  t->nrof = (uint16_t)value;
102  else if (sscanf(cp, "magic %d", &value))
103  t->magic = (uint8_t)value;
104  else if (!strcmp(cp, "yes"))
105  t->next_yes = loadTreasure(reader, filename);
106  else if (!strcmp(cp, "no"))
107  t->next_no = loadTreasure(reader, filename);
108  else if (!strcmp(cp, "end"))
109  return t;
110  else if (!strcmp(cp, "more")) {
111  t->next = loadTreasure(reader, filename);
112  return t;
113  } else
114  LOG(llevError, "Unknown treasure-command: '%s', last entry %s in %s:%d\n", cp, t->name ? t->name : "null", filename.c_str(), bufferreader_current_line(reader));
115  }
116  LOG(llevError, "treasure lacks 'end' in %s at line %d.\n", filename.c_str(), bufferreader_current_line(reader));
118  return t;
119 }
120 
126 void TreasureLoader::load(BufferReader *reader, const std::string& filename) {
127  char *buf, name[MAX_BUF];
128  treasure *t;
129 
130  while ((buf = bufferreader_next_line(reader)) != NULL) {
131  if (*buf == '#' || *buf == '\0')
132  continue;
133 
134  if (sscanf(buf, "treasureone %s", name) || sscanf(buf, "treasure %s", name)) {
135  treasurelist *tl = (treasurelist *)calloc(1, sizeof(treasurelist));
136  tl->name = add_string(name);
137  tl->items = loadTreasure(reader, filename);
138 
139  /* This is a one of the many items on the list should be generated.
140  * Add up the chance total, and check to make sure the yes & no
141  * fields of the treasures are not being used.
142  */
143  if (!strncmp(buf, "treasureone", 11)) {
144  for (t = tl->items; t != NULL; t = t->next) {
145  if (t->next_yes || t->next_no) {
146  LOG(llevError, "Treasure %s is one item, but on treasure %s\n", tl->name, t->item ? t->item->name : t->name);
147  LOG(llevError, " the next_yes or next_no field is set\n");
148  }
149  tl->total_chance += t->chance;
150  }
151  }
152 
153  m_treasures->define(tl->name, tl);
154  } else
155  LOG(llevError, "Treasure-list didn't understand: %s in %s:%d\n", buf, filename.c_str(), bufferreader_current_line(reader));
156  }
157 }
global.h
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
get_empty_treasure
static treasure * get_empty_treasure(void)
Definition: TreasureLoader.cpp:39
Archetypes
Definition: Archetypes.h:27
llevError
@ llevError
Definition: logger.h:11
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
TreasureLoader.h
SEE_LAST_ERROR
@ SEE_LAST_ERROR
Definition: define.h:52
npc_dialog.filename
filename
Definition: npc_dialog.py:99
treasurestruct
Definition: treasure.h:63
free_string
void free_string(sstring str)
Definition: shstr.c:280
Treasures
Definition: Treasures.h:22
TreasureLoader::m_treasures
Treasures * m_treasures
Definition: TreasureLoader.h:38
Utils.h
TreasureLoader::loadTreasure
treasure * loadTreasure(BufferReader *reader, const std::string &filename)
Definition: TreasureLoader.cpp:67
AssetsCollection::define
T * define(const Key &name, T *asset)
Definition: AssetsCollection.h:97
nroftreasures
size_t nroftreasures
Definition: assets.cpp:83
fatal
void fatal(enum fatal_error err)
Definition: utils.c:597
compat.h
TreasureLoader::m_archetypes
Archetypes * m_archetypes
Definition: TreasureLoader.h:39
treasureliststruct::name
const char * name
Definition: treasure.h:83
AssetsCollection::get
T * get(const Key &name)
Definition: AssetsCollection.h:66
Treasures.h
MAX_BUF
#define MAX_BUF
Definition: define.h:35
TreasureLoader::TreasureLoader
TreasureLoader(Treasures *treasures, Archetypes *archetypes)
Definition: TreasureLoader.cpp:24
treasureliststruct::total_chance
int16_t total_chance
Definition: treasure.h:84
treasureliststruct::items
struct treasurestruct * items
Definition: treasure.h:89
Floor.t
t
Definition: Floor.py:62
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
autojail.value
value
Definition: autojail.py:6
Archetypes.h
buf
StringBuffer * buf
Definition: readable.c:1606
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
BufferReader
Definition: bufferreader.c:21
TreasureLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: TreasureLoader.cpp:126
treasureliststruct
Definition: treasure.h:82
give.name
name
Definition: give.py:27