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 #include "AssetsTracker.h"
18 
19 extern "C" {
20 #include "string.h"
21 #include "global.h"
22 #include "compat.h"
23 }
24 
26  : m_treasures(treasures), m_archetypes(archetypes), m_tracker(tracker) {
27 }
28 
29 extern size_t nroftreasures;
30 
43  char *buf, *cp, variable[MAX_BUF];
45  int value;
46 
47  nroftreasures++;
48  while ((buf = bufferreader_next_line(reader)) != NULL) {
49 
50  if (*buf == '#')
51  continue;
52  cp = buf;
53  while (isspace(*cp)) /* Skip blanks */
54  cp++;
55 
56  if (sscanf(cp, "arch %s", variable)) {
57  if (t->item) {
58  LOG(llevError, "treasure: duplicate 'arch' in %s:%d\n", filename.c_str(), bufferreader_current_line(reader));
59  }
60  t->item = m_archetypes->get(variable);
61  } else if (sscanf(cp, "list %s", variable)) {
62  if (t->name) {
63  LOG(llevError, "treasure: duplicate 'name' in %s:%d\n", filename.c_str(), bufferreader_current_line(reader));
64  free_string(t->name);
65  }
66  t->name = add_string(variable);
67  } else if (sscanf(cp, "change_name %s", variable))
68  t->change_arch.name = add_string(variable);
69  else if (sscanf(cp, "change_title %s", variable))
70  t->change_arch.title = add_string(variable);
71  else if (sscanf(cp, "change_slaying %s", variable))
72  t->change_arch.slaying = add_string(variable);
73  else if (sscanf(cp, "chance %d", &value))
74  t->chance = (uint8_t)value;
75  else if (sscanf(cp, "nrof %d", &value))
76  t->nrof = (uint16_t)value;
77  else if (sscanf(cp, "magic %d", &value))
78  t->magic = (uint8_t)value;
79  else if (!strcmp(cp, "yes"))
80  t->next_yes = loadTreasure(reader, filename);
81  else if (!strcmp(cp, "no"))
82  t->next_no = loadTreasure(reader, filename);
83  else if (!strcmp(cp, "end"))
84  return t;
85  else if (!strcmp(cp, "more")) {
86  t->next = loadTreasure(reader, filename);
87  return t;
88  } else
89  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));
90  }
91  LOG(llevError, "treasure lacks 'end' in %s at line %d.\n", filename.c_str(), bufferreader_current_line(reader));
93  return t;
94 }
95 
101 void TreasureLoader::load(BufferReader *reader, const std::string& filename) {
102  char *buf, name[MAX_BUF];
103  treasure *t;
104 
105  while ((buf = bufferreader_next_line(reader)) != NULL) {
106  if (*buf == '#' || *buf == '\0')
107  continue;
108 
109  if (sscanf(buf, "treasureone %s", name) || sscanf(buf, "treasure %s", name)) {
110  treasurelist *tl = (treasurelist *)calloc(1, sizeof(treasurelist));
111  tl->name = add_string(name);
112  tl->items = loadTreasure(reader, filename);
113 
114  /* This is a one of the many items on the list should be generated.
115  * Add up the chance total, and check to make sure the yes & no
116  * fields of the treasures are not being used.
117  */
118  if (!strncmp(buf, "treasureone", 11)) {
119  for (t = tl->items; t != NULL; t = t->next) {
120  if (t->next_yes || t->next_no) {
121  LOG(llevError, "Treasure %s is one item, but on treasure %s\n", tl->name, t->item ? t->item->name : t->name);
122  LOG(llevError, " the next_yes or next_no field is set\n");
123  }
124  tl->total_chance += t->chance;
125  }
126  }
127 
128  tl = m_treasures->define(tl->name, tl);
129  if (m_tracker) {
131  }
132  } else
133  LOG(llevError, "Treasure-list didn't understand: %s in %s:%d\n", buf, filename.c_str(), bufferreader_current_line(reader));
134  }
135 }
global.h
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
Archetypes
Definition: Archetypes.h:27
llevError
@ llevError
Definition: logger.h:11
TreasureLoader::TreasureLoader
TreasureLoader(Treasures *treasures, Archetypes *archetypes, AssetsTracker *tracker)
Definition: TreasureLoader.cpp:25
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
treasureliststruct::name
sstring name
Definition: treasure.h:83
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:23
AssetsTracker
Definition: AssetsTracker.h:28
TreasureLoader::m_tracker
AssetsTracker * m_tracker
Definition: TreasureLoader.h:40
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:42
AssetsCollection::define
T * define(const Key &name, T *asset)
Definition: AssetsCollection.h:97
nroftreasures
size_t nroftreasures
Definition: assets.cpp:84
fatal
void fatal(enum fatal_error err)
Definition: utils.c:580
compat.h
TreasureLoader::m_archetypes
Archetypes * m_archetypes
Definition: TreasureLoader.h:39
AssetsTracker::assetDefined
virtual void assetDefined(const archetype *asset, const std::string &filename)
Definition: AssetsTracker.h:36
AssetsTracker.h
AssetsCollection::get
T * get(const Key &name)
Definition: AssetsCollection.h:66
Treasures.h
MAX_BUF
#define MAX_BUF
Definition: define.h:35
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
get_empty_treasure
treasure * get_empty_treasure(void)
Definition: treasure.c:1353
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:1610
BufferReader
Definition: bufferreader.c:21
TreasureLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: TreasureLoader.cpp:101
treasureliststruct
Definition: treasure.h:82
give.name
name
Definition: give.py:27