Crossfire Server, Trunk
ResourcesManager.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 <QString>
14 #include <qlist.h>
15 #include <qhash.h>
16 #include <QStringList>
17 #include <QMessageBox>
18 #include "ResourcesManager.h"
19 #include <locale.h>
20 
21 extern "C" {
22 #include "global.h"
23 #include "libproto.h"
24 #include "recipe.h"
25 #include "image.h"
26 #include "sproto.h"
27 }
28 #include "assets.h"
29 #include "AssetsManager.h"
31 #include "random_maps/RandomMap.h"
32 #include "LicenseManager.h"
33 #include "ArchetypeWriter.h"
34 #include "QuestWriter.h"
35 #include "MessageWriter.h"
36 #include "TreasureWriter.h"
37 #include "ArtifactWriter.h"
40 #include "faces/FaceWrapper.h"
42 
43 ResourcesManager::ResourcesManager() : myMapInformationManager(nullptr), myArchetypes(new ArchetypeWriter()), myQuests(new QuestWriter()), myTreasures(new TreasureWriter()),
44  myGeneralMessages(new MessageWriter()), myArtifacts(new ArtifactWriter())
45 {
46 }
47 
49 {
50 }
51 
52 static void onFatalInit(enum fatal_error) {
53  QMessageBox::critical(nullptr, "Fatal error", "Error while initializing Crossfire data, make sure you have maps and archetypes correctly installed.");
54 }
55 
57 {
58  setlocale(LC_NUMERIC, "C");
59 
62  static std::function<void(BufferReader *, const char *)> dummy = [this] (BufferReader *reader, const char *filename) { myLicenseManager.readLicense(reader, filename); };
63  settings.hooks[settings.hooks_count] = [] (BufferReader *reader, const char *filename) { dummy(reader, filename); };
66  settings.hooks[settings.hooks_count] = [] (BufferReader *, const char *) { QCoreApplication::processEvents(); };
70  init_globals();
71  init_library();
72  settings.fatal_hook = nullptr;
73  init_gods();
74  init_readable();
75 
76  QString key;
77 
78  for (int ing = 1; ; ing++)
79  {
81  if (!list)
82  break;
83 
84  QHash<QString, recipestruct*> recipes;
85  for (recipestruct* rec = list->items; rec; rec = rec->next)
86  {
87  key = QString("%1_%2").arg(rec->arch_name[0], rec->title);
88  recipes[key] = rec;
89  }
90  myRecipes.append(recipes);
91  }
92 }
93 
95 {
96  return myRecipes.size();
97 }
98 
99 QStringList ResourcesManager::recipes(int count) const
100 {
101  if (count < 1 || count > myRecipes.size())
102  return QStringList();
103 
104  QStringList keys = myRecipes[count - 1].keys();
105  qSort(keys);
106  return keys;
107 }
108 
109 const recipe* ResourcesManager::getRecipe(int ingredients, const QString& name) const
110 {
111  if (ingredients < 1 || ingredients > myRecipes.size())
112  return NULL;
113 
114  return myRecipes[ingredients - 1][name];
115 }
116 
118 {
119  bool goOn = true;
120  getManager()->archetypes()->each([&item, &callback, &goOn] (archt *arch) {
121  if (!goOn)
122  return;
123 
124  if (arch->clone.other_arch == item)
125  {
126  goOn = callback(OTHER_ARCH, arch, nullptr, nullptr, nullptr);
127  }
128 
129  sstring death_anim = NULL;
130  if (goOn && (death_anim = object_get_value(&arch->clone, "death_animation")) && strcmp(death_anim, item->name) == 0)
131  {
132  goOn = callback(DEATH_ANIM, arch, nullptr, nullptr, nullptr);
133  }
134  });
135 
136  getManager()->treasures()->each([&item, callback, &goOn] (treasurelist *list) {
137  if (!goOn)
138  return;
139  for (auto t = list->items; t; t = t->next)
140  {
141  if (t->item == item)
142  {
143  goOn = callback(TREASURE_USE, nullptr, list, nullptr, nullptr);
144  }
145  }
146  });
147 
148  QList<CREMapInformation*> mapuse = store->getArchetypeUse(item);
149  foreach(CREMapInformation* information, mapuse)
150  {
151  if (!goOn)
152  continue;
153  goOn = callback(MAP_USE, nullptr, nullptr, information, nullptr);
154  }
155  auto allMaps = store->allMaps();
156  foreach(CREMapInformation *information, allMaps)
157  {
158  if (!goOn)
159  return;
160  foreach(RandomMap* rm, information->randomMaps())
161  {
162  if (strcmp(item->name, rm->parameters()->final_exit_archetype) == 0)
163  {
164  goOn = callback(RANDOM_MAP_FINAL_EXIT, nullptr, nullptr, information, nullptr);
165  }
166  if (!goOn)
167  return;
168  }
169  }
170 
171  int count = 1;
172  recipelist* list;
173  while ((list = get_formulalist(count++)))
174  {
175  if (!goOn)
176  break;
177  recipestruct* rec = list->items;
178  while (goOn && rec)
179  {
180  for (size_t ing = 0; ing < rec->arch_names; ing++)
181  {
182  if (strcmp(rec->arch_name[ing], item->name) == 0)
183  {
184  goOn = callback(ALCHEMY_PRODUCT, nullptr, nullptr, nullptr, rec);
185  break;
186  }
187  }
188  rec = rec->next;
189  }
190  }
191 }
192 
194  myArchetypes.assetModified(arch);
195 }
196 
198  myArchetypes.saveModifiedAssets();
199 }
200 
203 }
204 
207 }
208 
210  myTreasures.assetModified(treasure);
211 }
212 
214  myTreasures.saveModifiedAssets();
215 }
216 
219 }
220 
223 }
224 
226  myArtifacts.saveModifiedAssets();
227 }
ResourcesManager::treasureModified
void treasureModified(treasurelist *treasure)
Definition: ResourcesManager.cpp:209
AssetUseCallback
std::function< bool(ArchetypeUse use, const archt *, const treasurelist *, const CREMapInformation *, recipe *)> AssetUseCallback
Definition: ResourcesManager.h:77
recipestruct::arch_names
size_t arch_names
Definition: recipe.h:12
ResourcesManager::myQuests
ModifiedAssetsManager< quest_definition > myQuests
Definition: ResourcesManager.h:162
RandomMap
Definition: RandomMap.h:26
global.h
RandomMap::parameters
const RMParms * parameters() const
Definition: RandomMap.cpp:39
get_formulalist
recipelist * get_formulalist(int i)
Definition: recipe.c:96
Settings::hooks_count
uint8_t hooks_count
Definition: global.h:327
GeneralMessage
Definition: book.h:44
ArchetypeWriter.h
ResourcesManager::ResourcesManager
ResourcesManager()
Definition: ResourcesManager.cpp:43
Settings::archetypes_tracker
void * archetypes_tracker
Definition: global.h:331
ArchetypeWriter
Definition: ArchetypeWriter.h:22
ResourcesManager::archetypeUse
static void archetypeUse(const archt *item, CREMapInformationManager *store, AssetUseCallback callback)
Definition: ResourcesManager.cpp:117
ResourcesManager::getRecipe
const recipestruct * getRecipe(int ingredients, const QString &name) const
Definition: ResourcesManager.cpp:109
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
ResourcesManager::saveArchetypes
void saveArchetypes()
Definition: ResourcesManager.cpp:197
AssetsManager.h
ResourcesManager::questModified
void questModified(quest_definition *quest)
Definition: ResourcesManager.cpp:201
QuestWriter.h
ResourcesManager::myArtifacts
ModifiedAssetsManager< artifact > myArtifacts
Definition: ResourcesManager.h:165
MessageWriter.h
guildoracle.list
list
Definition: guildoracle.py:87
recipe.h
ArtifactWriter.h
ObjectWrapper.h
hall_of_fame.keys
keys
Definition: hall_of_fame.py:43
ResourcesManager::myLicenseManager
LicenseManager myLicenseManager
Definition: ResourcesManager.h:181
ResourcesManager::recipeMaxIngredients
int recipeMaxIngredients() const
Definition: ResourcesManager.cpp:94
RandomMap.h
ResourcesManager::saveQuests
void saveQuests()
Definition: ResourcesManager.cpp:205
npc_dialog.filename
filename
Definition: npc_dialog.py:99
add_server_collect_hooks
void add_server_collect_hooks()
Definition: init.c:1092
Settings::fatal_hook
fatalHook fatal_hook
Definition: global.h:332
getManager
AssetsManager * getManager()
Definition: assets.cpp:335
treasurestruct
Definition: treasure.h:63
LicenseManager.h
ResourcesManager::myGeneralMessages
ModifiedAssetsManager< GeneralMessage > myGeneralMessages
Definition: ResourcesManager.h:164
archt
Definition: object.h:469
settings
struct Settings settings
Definition: init.c:39
init_readable
void init_readable(void)
Definition: readable.c:903
object_get_value
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4317
init_gods
void init_gods(void)
Definition: holy.cpp:63
ResourcesManager::saveArtifacts
void saveArtifacts()
Definition: ResourcesManager.cpp:225
quest
Definition: quest.py:1
CREMapInformation
Definition: CREMapInformation.h:27
recipestruct
Definition: recipe.h:10
AssetsTracker
Definition: AssetsTracker.h:28
ModifiedAssetsManager::saveModifiedAssets
void saveModifiedAssets()
Definition: ModifiedAssetsManager.h:47
AssetsManager::treasures
Treasures * treasures()
Definition: AssetsManager.h:54
ResourcesManager::saveGeneralMessages
void saveGeneralMessages()
Definition: ResourcesManager.cpp:221
ResourcesManager::load
void load()
Definition: ResourcesManager.cpp:56
recipestruct::arch_name
char ** arch_name
Definition: recipe.h:13
AnimationWrapper.h
nlohmann::detail::cbor_tag_handler_t::store
@ store
store tags as binary type
ArchetypeWrapper.h
ArtifactWriter
Definition: ArtifactWriter.h:22
ResourcesManager::recipes
QStringList recipes(int count) const
Definition: ResourcesManager.cpp:99
LicenseManager::readLicense
void readLicense(BufferReader *reader, const char *filename)
Definition: LicenseManager.cpp:27
QuestWriter
Definition: QuestWriter.h:19
sstring
const typedef char * sstring
Definition: global.h:40
disinfect.count
int count
Definition: disinfect.py:7
FaceWrapper.h
Settings::hooks_filename
const char * hooks_filename[20]
Definition: global.h:328
sproto.h
recipeliststruct
Definition: recipe.h:37
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:135
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
init_globals
void init_globals(void)
Definition: init.c:265
image.h
MAP_USE
@ MAP_USE
Definition: ResourcesManager.h:72
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
CREMapInformationManager.h
ResourcesManager.h
ResourcesManager::archetypeModified
void archetypeModified(archetype *arch)
Definition: ResourcesManager.cpp:193
ResourcesManager::myRecipes
QList< QHash< QString, recipestruct * > > myRecipes
Definition: ResourcesManager.h:160
CREMapInformation::randomMaps
QList< RandomMap * > randomMaps() const
Definition: CREMapInformation.cpp:258
RMParms::final_exit_archetype
char final_exit_archetype[RM_SIZE]
Definition: random_map.h:59
Floor.t
t
Definition: Floor.py:62
MessageWriter
Definition: MessageWriter.h:22
ModifiedAssetsManager::assetModified
void assetModified(T *asset)
Definition: ModifiedAssetsManager.h:45
item
Definition: item.py:1
quest_definition
Definition: quest.h:42
onFatalInit
static void onFatalInit(enum fatal_error)
Definition: ResourcesManager.cpp:52
assets.h
recipestruct::next
struct recipestruct * next
Definition: recipe.h:24
Settings::hooks
collectorHook hooks[20]
Definition: global.h:329
TreasureWriter
Definition: TreasureWriter.h:22
ALCHEMY_PRODUCT
@ ALCHEMY_PRODUCT
Definition: ResourcesManager.h:74
ResourcesManager::generalMessageModified
void generalMessageModified(GeneralMessage *message)
Definition: ResourcesManager.cpp:217
castle_read.key
key
Definition: castle_read.py:64
RANDOM_MAP_FINAL_EXIT
@ RANDOM_MAP_FINAL_EXIT
Definition: ResourcesManager.h:73
fatal_error
fatal_error
Definition: define.h:47
ResourcesManager::myArchetypes
ModifiedAssetsManager< archetype > myArchetypes
Definition: ResourcesManager.h:161
ResourcesManager::saveTreasures
void saveTreasures()
Definition: ResourcesManager.cpp:213
TreasureWriter.h
init_library
void init_library(void)
Definition: init.c:192
ResourcesManager::myTreasures
ModifiedAssetsManager< treasurelist > myTreasures
Definition: ResourcesManager.h:163
CREMapInformationManager
Definition: CREMapInformationManager.h:29
BufferReader
Definition: bufferreader.c:21
if
if(!(yy_init))
Definition: loader.c:2626
ResourcesManager::~ResourcesManager
virtual ~ResourcesManager()
Definition: ResourcesManager.cpp:48
libproto.h
treasureliststruct
Definition: treasure.h:82
give.name
name
Definition: give.py:27