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