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